Beginner Question: Finding a random point on a line trace
I read somewhere someone offering this as a solution for random spawning of actors, I can't find the example for the life me. My goal is to spawn actors randomly from a line trace originating from a character inside a static mesh building. Guards inside a castle'
I know one node should be a random float in range connected to a make a vector X, but how do i constrain that to a line trace?
Can someone show me an example that would work with 10+ actors spawning at the same time? My vector length is about 1000
this is me guessing
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.
Follow this question
Once you sign in you will be able to subscribe for any updates here