Sup. With this script below, my AI will stop chasing the character if he's jumping. Any way to have him chase the character, but remained grounded if the character's jumping? If I take out the "&& isJumping == false" he'll follow the character into the air :.
function Patrol(){
while(true){
if( AttackRangeCheck() ){ // target object is within range
if( !StopRangeCheck() && Walker.isJumping == false ){
transform.LookAt(GameObject.FindWithTag("Player").transform);
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
animation.CrossFade("run");
}
}
yield;
}
}
I can think of a hundred things but I guess I'll begin by mentionning that movement with Translate or position+=(xyz) does not take collisions or gravity into account. For most simple character movement, using the CharacterController along with SimpleMove() or Move() will do the trick. If you choose Move(), you'd then have to add a constant gravity to your object.
In case of a non-3d game where CharacterController/gravity/collisions aren't required, the simple way is to go:
var newPos = Vector3(GameObject.FindWithTag("Player").transform.position.x, transform.position.y, GameObject.FindWithTag("Player").transform.position.z);
transform.LookAt(newPos);
EDIT:
1-What we did there is to create a new position using the target x,z and our own y.
2-Reading the code, I'd suggest you hold a reference to the player into a variable as using .Find is 'slow'.
If I understand correctly what you're asking, you want the transform to follow the player, but only in X and Z -- you want his Y to stay constant. Your current script isn't doing that because the LookAt method is making him look up at the player, and so when he moves forward, he floats off the ground.
So there are two ways to handle this: don't make him look directly at the player, or don't make him move directly where he's looking.
Option 1: don't look up at the player. Replace your LookAt line with (In C#):
Vector3 playerPos = GameObject.FindWithTag("Player").transform;
playerPos.y = 0; // (ignore the player's y position)
transform.LookAt(playerPos);
Option 2: look at the player, but stay grounded. Replace your Translate line with: