x


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) {
        animation.CrossFade("threaten");
        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) {
        animation.CrossFade("attack");
        transform.LookAt (LookAtTarget); 
        transform.Translate (0,0,0);
    }
    else {
        animation.CrossFade("idle");
    }    
}
more ▼

asked Jan 25 '11 at 07:31 PM

El ChiNo gravatar image

El ChiNo
7 3 3 6

you might try in the transform.LookAt (LookAtTarget); part of your script put: transform.LookAt(Vector3(LookAtTarget.position.x, 0, LookAtTarget.position.z));

not sure if this will work at all thats why I did it as a comment. hope it helps anyway :)

Jan 25 '11 at 07:40 PM Scribe

Thanks Scribe, it works, but if the player jumps over the enemy, the Ai goes kinda crazy for that moment, it evens gets rotated 90 and layed down to face the player being over it.

Jan 25 '11 at 08:55 PM El ChiNo

use transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z)); instead. My guess is that the worlds y 0-point is below your floor. When X and Z gets very small the resulting vector points almost straight down. by replacing the "0" with transform.position.y the resulting vector always lay on the X-Z-plane relative to your enemy.

Jan 25 '11 at 09:54 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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)); 

instead of

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.

more ▼

answered Jan 25 '11 at 07:58 PM

Bunny83 gravatar image

Bunny83
46.8k 12 50 210

Sorry for not being clear with my question, the game is a 3d, the player has a third person controller, and the script posted above is for the enemy.

Jan 25 '11 at 08:50 PM El ChiNo

Thanks Scribe and Bunny83. It's working better than before. The Ai doesn't walk in the air anymore!. However, if the player jumps and stays over the enemy (like being stand on his head), the Ai still gets laydown on the ground, facing upward (being the player on top of him). I do not know how to avoid this.

Jan 26 '11 at 02:28 PM El ChiNo

Ok, i guess you want to rotate you enemy just around y-axis, right? I will edit the answer to avoid rotation around x ans z

Jan 28 '11 at 09:01 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x982
x372

asked: Jan 25 '11 at 07:31 PM

Seen: 1440 times

Last Updated: Jan 25 '11 at 07:31 PM