Static Object Spawner
Last updated
Last updated
If you haven't read
yet, please do it and then come back here.
This object spawner is using the paths to spawn static objects. These objects will be using the anchor points of all the paths, including the initial anchor point. In this effect, the preferred step distance varies between a minimal and a maximal value, to allow for a more organic feeling.
A lot of properties allow to custom that effect further, and it could be expended even more.
Objects Count To Spawn
uint
The amount of objects that will be spread among all the paths
Min Preferred Step Distance
float >= 0.01
The distance between each step will be, if possible, between the min and max step distance.
Max Preferred Step Distance
float >= Min Preferred Step Distance
The distance between each step will be, if possible, between the min and max step distance.
Should Randomize Rotation
bool
If true, will add a random rotation to the spawned object along the up axis
Parent Spawned Object
bool
If true, the spawned objects will be a child of the object where the anchor point was found
Normal Shift
float
Use this to give an offset along the normal to the spawned objects. Note that there is already a constant offset that is setup in the AnchorPoint.cs script
Normal Axis For Spawned Objects
Transform
Can be use to make object spawn on a specific sides of objects
Scalar Product Tolerance
float [-1;1]
The closer to 1, the more the anchor points' normal has to be close to the Normal Axis For Spawned Objects for the object to be spawned
Let's illustrate what the non obvious parameters do when we tweak them. For that let's use a static object spawner that spawns flowers, because after the ants from the previous page, we need that.
Now as you know, this class inherits from the object spawner, so let's consider these properties:
Layer Mask : Default
Number of Paths : 10
Divergence : 0
Angle Range: 360
Iteration Angle Divergence : 0
Are Paths Sorted : True
Spawnables : Flowers
Random Spawn Delay : 3
And let's start with these properties for the component itself:
Objects Count To Spawn: 31 (Note that this will define the amount of anchor points per path)
Min Preferred Step Distance : 1
Max Preferred Step Distance : 1
Should Randomize Rotation : false
Parent Spawned Object: false
Normal Shift: 0
Normal Axis For Spawned Objects: None (ignored)
Scalar Product Tolerance: -1 (ignored)
WIth these parameters, we get 31 flowers that are scattered on 10 paths. Each of these paths has segments of length 1. This is the result that we get:
Note what happens with the same parameters, but if we don't sort the paths for that effects :
Now If we change to have the minimum distance to 0.5 and the maximum to 1.5, we can obtain such results. Note that all the paths have different lengths now.
Randomizing the rotation is just applying a random rotation to the spawned object around their up vector, although the flowers have a lot of symmetry axes, it still shows in that example if you look attentively:
If you set the property Parent Spawned Object to true then the instantiated objects will be parented to the transform that holds the collider where the anchor point was found. Otherwise the instantiated objects will be set at the top of the hierarchy.
It is important to have it on if you want to spawn objects on moving colliders, see the difference:
The Normal Shift is how much should the spawned object be translated along the normal when it is spawned. This can be convenient in different use cases, and can look like this with our flowers who are now standing a bit above the ground:
Now the last two parameters Normal Axis For Spawned Objects and Scalar Product Tolerance. The first one is a transform and we are interested in its up vector. If we take such a transform and set a Scalar Product Tolerance to 0.9, then what will happen is that the objects with a very similar up vector will be spawned. The closer the Scalar Product Tolerance to 1 the more the objects has to match the up vector of the given transform.
But why?
Well imagine you want to spawn trees with a static object spawner. You might not want your trees to end up sideways or upside down. You want them to be somewhat having their roots in the ground and their top looking at the sky. These parameters make that possible, as you can see on these two different examples: