This following code does work, but I have one issue with it. Once the gameobject starts getting close to the player that it is chasing it slows down and does not continue going at a constant speed to the target. Is there another way I can code this to keep the speed constant even if the gameobject is close to the target?

Vector3 followXonly = new Vector3(target_position.x, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, followXonly, speed * Time.deltaTime);

Unity provides a helper method to deal with exactly this kind of situation:

Vector3 followXonly = new Vector3(

transform.position = Vector3.MoveTowards(
                            speed * Time.deltaTime);

Vector3.MoveTowards handles advancing the position in the direction of the target at a constant speed regardless of distance, and also guards against overshooting when your object is less than one full frame duration away from its destination.

| improve this answer | |

It slows down because your transformation is a linear interpolation of a non-normalized vector. That is: as the distance between where the object is now and its target gets shorter, the speed will slow down because you're only moving (say) 5% of that distance every frame.

You get this:


It's like taking 1 and multiplying by 0.95 20 times and expecting to get 0 (it's actually 0.35848 and a bit).

You need to replace the speed * Time.deltaTime portion with a counter that you increment by deltaTime every frame and the transform.position with its starting location. That way the Lerp is actually Linear (because you're supplying it with a linearly increasing value, rather than a constant).

Or refactor the code to be something more like:

Vector3 followXonly = new Vector3(target_position.x - transform.position.x, transform.position.y, transform.position.z);
followXonly = followXonly.normalized * speed;
transform.position = transform.position + followXonly;
| improve this answer | |
  • \$\begingroup\$ Ahh ok I see I should of realized lerp had more to it then I assumed. Thanks for breaking it down for me. Unfortunately the code given did not work but thats ok I enjoy learning and your breakdown about it! Very informative! \$\endgroup\$ – Sol Apr 11 '16 at 3:22
  • \$\begingroup\$ The glitch in the code above is that it's missing a ` - transform.position`. The vector you want to normalize is the difference between the target position and the current position. Note that this version will still tend to overshoot and oscillate when it reaches the target unless you handle that case specifically. \$\endgroup\$ – DMGregory Apr 11 '16 at 17:50
  • \$\begingroup\$ Oops, yep. Thanks @DMGregory I was writing it off the top of my head and the edit window isn't the greatest place to write code. \$\endgroup\$ – Draco18s no longer trusts SE Apr 11 '16 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.