You're trying to spawn creatures at random along a line segment? That's pretty straight forward... Here are the things you already should know:

The start vertex

The end vertex

The length of the segment pseudo-code (end - start).Length()

Are there any obstacles blocking the line segment? To find out, this is where you would use the line trace. Look at the hit result and see if there are any hits.

Now, you can generate ten random numbers between 0 and the segment length. These will be the spawn distances from the start vertex.

Next, we need to find the actual spawn positions in 3d space along the line segment. This might be the part which confuses you. You have to do SegmentEnd - SegmentStart to get a vector which has an origin at [0,0,0]. The direction of this vector will be the same direction as your segment. To get the LOCAL spawn position along this vector: Normalize this vector, then multiply it by the random numbers you generated. This is a randomly scaled position along the local vector. We're not done yet. Now, you have to translate this position from local space to world space by adding the SegmentStart position to the randomly generated point. Now, this point is in 3D space along the line segment.

We're still not done though! The actual spawn position may not exactly be on this line segment. If your terrain has a valley or a hill, you want to spawn your creature along the contour of the terrain along this line. To do this, you do another line trace from a high position (spawnpos + (0,0,1000)) to a reasonably low position (spawnpos - (0,0,1000)). Find the intersection point -- this should be your terrain or some other object. This is the final spawn position for your creature.

answered **
Sep 22 '15 at 12:59 AM **

Slayemin

819
●
49
●
29
●
73