Enemy walks through hills and flies in the air

I can’t get the enemy to walk along the floor and walk over the hills. The enemy is set to follow the character by using this script:

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

var myTransform : Transform; //current transform data of this enemy

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;


}

but it causes the enemy to walk at the same height as the players head and so he walks through the hills to get to the player. I have set the enemy to have a rigidbody, set with gravity and kinematic on. How can I sort this out? Any help will be appreciated. Sorry if it’s a really stupid thing, it’s just I am new to Unity and need help

Hi MrrMaddMann,

There are a few things that need looking at here. First check the centre point for the GameObject your enemy is following.

In the past I have had to set a specific object to act as the actual part of the GameObject that should followed. So instead of following the root/parent of the gamobject, have a child that will actually be the target to follow. You can adjust the position of the child to offset any problems and allow greater flexibility.

Hope that makes sense, as I’ve not had my coffee yet :wink: