- Home /
Push player in direction based on objects movement
In my game I have monsters that push the player around when they get in range. Pushing the player backwards with a front on attack is easy enough
player.transform.Translate(0,0,-1)
but doesn't work so well when the monsters attack you from behind (you end up getting pushed through the monster, which triggers the push function several times and you could end up anywhere).
Is there a way to detect the direction the monster is moving in and push the player in the same direction? Keeping in mind the push function is based on distance from the player and not a collision.
Answer by Jesse Anders · Oct 12, 2010 at 02:42 PM
You probably want to push the player in the direction of the vector from the monster's position to the player's position, e.g.:
Vector3 direction = player.transform.position - transform.position;
direction.Normalize();
player.transform.Translate(direction * pushAmount * Time.deltaTime, Space.World);
Normalize! That was the code I was missing. Cheers mate, works a charm.
Your answer
Follow this Question
Related Questions
How can I move the character like 2D games? 1 Answer
What movment does the character do 1 Answer
Have a character face their moving direction? 1 Answer
is there any better way to move the character than rigidbody.addRelativeForce? 1 Answer
Third Character Movement , Slide On Terrain and other object. 0 Answers