Spawning a series of objects in-front of the player
Hello,
I'm trying to get a series of objects to spawn in front of the player based on the direction they are facing and so far it keeps spawning them on the Z-axis and not based on the player's direction. I've tried everything and I just don't know whats causing it not to work properly.
Currently, im using the transform.forward to get the direction and * it by a separation value + the original player positions but it's just not working.
I've looked into other similar issues and even when applying their solutions it doesn't work.
Any help is great! The code below, thanks!
private void CalculatePath()
{
impactPositions.Clear();
Vector3 initialPos = transform.position;
Vector3 playerDirection = transform.forward;
int sep = separation;
for (int i = 0; i < amountOfImpacts; i++)
{
Vector3 spawnPos = transform.position + playerDirection * sep;
impactPositions.Add(spawnPos);
sep += separation;
}
}
Answer by eses · Aug 21, 2018 at 02:25 PM
Hi @alexlem_17
Note: Your "keeps spawning them on the Z-axis and not based on the player's direction." does not make sense based on your code. Your objects do spawn in front of player, but they just stack on each other. Also "sep += separation" does not make sense for me, see the solution below.
Also, I doubt you have used Debug draw ray. Have you at least tried instantiating GameObject and see where they end up?
You should visualize your vectors.
Your spawnPos is transform position.
You add to your spawn pos a "stick" pointing towards player forward direction.
Let's say player looks towards NE (1,0,1). This is normalized so that this vector is of length 1, so the vector tip is at roughly (0.7,0,0.7)
If your player is at 0,0,0 and you add this stick/vector to it:
You'll end up in (0.7,0,0.7)
But now you actually just multiply this by sep. If it is 10, then you'll end up each time at: (7,0,7)
So where is the scattering. If you just add to it, you are moving along line, further away along vector's line.
You should add direction multiplied by distance you want for scatter center to your player position. Then when you have this position, you should add some random position vector offset to this point. That way your new vectors give you random points around this point.
You could also take some other route to scatter items in front of player, but this is close to what you explained I think.
Hey, thanks for the reply
I've done a number of debug.rays to help figure things out and also the sep += separation is because I'm spawning multiple objects that have a pre-defined separation gap and this just adds the gap for each of them.
I'll try applying what I've understood from your answer, thanks!
In regards to the first part of your answer, my objects are not spawning on top of one another and do form the line with the amount of separation I want. It's just not going in the correct direction, maybe it's another part of my code if this part looks like it should go in the correct direction.
Your answer
Follow this Question
Related Questions
Tilt/Rotation based on direction of movement? (player follows mouse hover) 0 Answers
move object in a certain direction 0 Answers
How to change the direction of the model? 1 Answer
Controlling Angle of Movement When Holding 2 Arrow Keys - 2d Top Down 0 Answers
Lerp between two direction vector 0 Answers