Follow AI Script , AI gets launched

Hi i’m having an issue on every follow ai script i try on my AI.
I have a zombie thats supposed to chase me when i get near but instead of following me when in range, it gets launched as soon as i get close.
This is the code i’m using currently

 var target : Transform;
 var moveSpeed = 20;
 var rotationSpeed = 5;
 var myTransform : Transform;
 var minDistance = 50;
      
 function Awake()
 {
     myTransform = transform;
 }
      
 function Start()
 { 
     target = GameObject.FindWithTag("Player").transform;
 }
      
 function Update () {
      if(Vector3.Distance(myTransform.position, target.position) < minDistance) {
        
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
        Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
 }

That code should move the zombie 20 units per second, as the moveSpeed is 20. With one unit generally being 1 meter, that would be 20 meters each second. That’s 72 km/h or if you’re American, ~45mph.

Unless your zombies are actually zombie cars, or everything is at a really large scale, I’d suggest tuning the movespeed back a bit.

At the same time the rotation speed is very low. You’re rotating the zombie 5 degrees every second, which means that it will take it 72 (360/5) seconds to turn completely around.

So, if you approach the zombie from behind, it will zoom away from you at the speed of you average car on a highway, and turn to face you over the duration of about half a minute. Which isn’t really what I’d call “chasing”.

If your models are human sized (a bit less than two standard Unity cubes tall), a good rotation speed is usually in the ballpark of 180-360 degrees per second, and movement speeds in the range of 3-6 seems natural. All depending on your application.

Finally, I’d advice you to not move the zombie directly forward, but instead towards the character. If you approach it from behind, it’ll look a bit weird, but not as weird as if the zombie starts walking away from you in a little loop. So your movement code would be:

var toTarget = (target.position - myTransform.position).normalized;
myTransform.position += toTarget * moveSpeed * Time.deltaTime;