# How to avoid enemies walk up in the air

Hi everyone, I have a little problem that got stucked in a different question posted earlier this week. I have an enemy that follows the player when he's at a certain distance. The thing is that when the enemy is chasing the player, if he jumps, the Ai start to walk in the Y axis trying to reach the player in the air. How can I make the enemy follow the player, without walking in the air, keeping him on the ground, no matter if the player jumps or not?

Thanks!

Here's the script so far:

``````var LookAtTarget:Transform;
var speed = 3.0;
var proximity = 3.0;
var maxDistance = 1.0;

function Start () {
animation.wrapMode = WrapMode.Loop;
}

function Update(){
var dist = Vector3.Distance(LookAtTarget.transform.position, transform.position);

//check whether you are within target proximity
if ( dist < proximity && dist > maxDistance) {
transform.LookAt (LookAtTarget); //you look at player with this
transform.Translate (0,0,speed*Time.deltaTime);
//moves with speed in local z and you are looking toward him so you move toward the player
//write other codes here
}
else if (dist < maxDistance) {
transform.LookAt (LookAtTarget);
transform.Translate (0,0,0);
}
else {
}
}

``````

Like Scribe mentioned you should eliminate the y-part of the direction. That way your enemy turns just around the y-axis. I don't know what kind of game you try the make 2D sidescroll or 3D but anyway, it would be better to move your enemy with a charactercontroller or rigidbody. As far as i know if you set the position directly via Transform you bypass any collision detection. Add a characterController and use it's Move() function (works similar to Transform.Translate()). That would apply gravity to your enemy and it can walk ramps up and down.

edit

``````transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));

``````

``````transform.LookAt(Vector3(LookAtTarget.position.x, 0, LookAtTarget.position.z));

``````

edit

``````transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));
transform.localEulerAngles = Vector3(0,transform.localEulerAngles.y,0);

``````

The second line just eliminates the rotation around x and z. Now it's impossible for the enemy to rotate around these axes.

var target:Transform;

var rotationSpeed:int=2;

function Awake(){
myTransform = transform;

}

var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);

what you want is the character to stop rotating on the y axis the y is up z is back and forth and x is side to side i had the same problem this solved it 100 percent