- Home /
2D Enemy chasing player not at a constant speed,Enemy following player at constant speed
In my 2D game, I have enemies chasing the player whenever he gets too close to them.The problem is, they start to slow down the closer they get to the player. Here's the code I currently have regarding the chase:
if (Vector3.Distance (transform.position, player.transform.position) < 1) {
xAxis = player.transform.position.x - transform.position.x;
yAxis = player.transform.position.y - transform.position.y;
body.velocity = new Vector3 (xAxis, yAxis, 0f);
moving = true;
attacking = true;
When the condition is false, the enemies move normally. I think they slow down because the closer they get to the player, the lower the vector input is. How can I change it so they chase the player at a constant speed, regardless of whether the distance is .9f or .09f?
Your answer
Follow this Question
Related Questions
Movement Relative to Rotated Parent 1 Answer
Velocity in Unity2D 2 Answers
Moving colliders that are part of a composite collider 1 Answer
How to touch anywhere in the screen and the point touched should alwaysstart from a particular point 1 Answer
Vector3 Issues - Not Able To Solve Constructor issue 1 Answer