0
votes

I have a pyramid that is supposed to move in front of the player after a set period of time and distance. If the player is looking the direction that are moving, everything works fine. If the player moves one direction and looks another, than they never encounter the pyramid. What do I need to do to get the pyramid to move in front of their movement and not their camera? Here's my current code for the movement:

Pyramid.transform.position = Player.transform.position + Player.transform.forward * PyramidToPlayerMoveDistance; 
        Pyramid.transform.LookAt(Player.transform, Vector3.up);
1

1 Answers

0
votes

If you want to position your "pyramid" towards the movement of the player you could find the direction of movement. To do so you firstly need to find the velocity.The velocity is the difference between the new position and the last position.

velocity = newPlayerPos - oldPlayerPos

The vector direction is the normalized velocity.

direction = velocity.normalized

Then position the pyramid

Pyramid.transform.position = Player.transform.position + direction * PyramidToPlayerMoveDistance; 

This is not tested, but supposed to work. Also I would recommend to use Linear interpolation to smooth the movement.

Vector3.Lerp(oldPyramidPost, newPyramidPos, Time.deltaTime * speed)