- Home /
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"); }
}
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 :)
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.
use transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z)); ins$$anonymous$$d. $$anonymous$$y 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.
Answer by Bunny83 · Jan 25, 2011 at 07:58 PM
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.
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.
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.
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
Answer by rasheedqw · Sep 15, 2013 at 07:01 AM
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
Your answer
Follow this Question
Related Questions
Too Close To The Trap and melee attacking enemys. 1 Answer
SphereCastAll sees for miles 2 Answers
Adding Distance in javascript 2 Answers
Make Navmesh agent follow closest object with tag 0 Answers
Adding a load level to this script. 1 Answer