Ai follow script problem

The ai follow script I’m using works fine but the only problem is it has no gravity attached to it so it will randomly float off the ground and fly to follow my character whenever I jump. What should I change to keep my enemy on the ground ?

var target : Transform;

var moveSpeed = 3;

var rotationSpeed = 3;

var myTransform : Transform;

function Awake()
myTransform = transform; //cache transform data for easy access/preformance

function Start()
target = GameObject.FindWithTag(“Player”).transform; //target the player


function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;


Take the Y component out of target.position - myTransform.position

Also, if your rotate code works, leave it, but in the future, you might want to check out Vector3.RotateTowards, it makes that a lot easier to comprehend what’s happening.

EDIT to show code for taking Y coordinate out:

var diff_vector = target.position - myTransform.position; 
var diff_without_y = new Vector3(diff_vector.x, 0, diff_vector.z);