Unity 2D Following player on the x-axis only
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?
This is the code I have but does not work as I hope.
Vector3 followXonly = new Vector3(target_position.x, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, followXonly, enemy.speed * Time.deltaTime);
Answer by KdRWaylander · Apr 11, 2016 at 08:17 AM
Hi,
It's because you're using Vector3.Lerp().
You have two solutions:
Use Vector3.MoveTowards which is the "real" method to move: http://answers.unity3d.com/questions/746532/vector3lerp-constant-speed-between-distance-change.html
Tweak your Lerp to make it go at the same speed (it's in JS but you should be able to translate): http://forum.unity3d.com/threads/constant-speed-with-vector3-lerp.10109/
Edit: And THIS should be interesting for you to read.
Your answer
Follow this Question
Related Questions
How would I load a new scene when a score that is scored in a string is reached? 2 Answers
Destroying an object with a specific name 0 Answers
Why do prefabs overlap each other? 0 Answers
Bullet destroy 2 game object intent one Unity 2D 0 Answers
2D Android Game Rapid Jumping Problem,Unity 2018.2.9f1 Android 2d Speed Jumping 0 Answers