- Home /
http://answers.unity3d.com/questions/135582/enemy-jitters-when-following.html
Enemy Nudging Towards Player
I've got my enemy to look at and chase the Player, but once he gets to the distance where he should stop chasing the Player, he'll do tiny nudges towards the Player, getting closer to him. Anyone have a clue why this is happening?
if(targeted){
var rotation = Quaternion.LookRotation(Player.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
ChasePlayer = gameObject.GetComponent("EnemyControl");
if(distanceToTarget >= 3){
isMoving = true;
}else{
isMoving = false;
}
}else{
isMoving = false;
}
//CHASE THE PLAYER//
if(isMoving && distanceToTarget >= 3){
animation.CrossFade("run",0.2);
var controller2 : CharacterController = GetComponent(CharacterController);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
controller2.SimpleMove(forward * 3);
}
Answer by JonnyHilly · Jun 26, 2011 at 06:02 PM
because once he reaches the player he stops moving (or moves backwards a little to get to the limit radius/range), then next frame, he is out of range again, so he moves in range again, and this repeats... hence the jittery movement.
You can fix it in several ways... 1) make it so the enemy cant move faster than the player. 2) make it so the speed he chases the player is variable speed, not just true or false for chasing the player (more speed when further) less speed when very close (match player's speed)
You'll have to be more specific because I tried what you said and it still jitters.
Follow this Question
Related Questions
Getting enemy to move towards player untill they collide problem... 2 Answers
Enemy raycast chase player 0 Answers
When the enemy character shoots, the bullet won't go to the position of my player! 2 Answers
If Player Looks At Enemy Script 4 Answers
How to make my player not lose health when attacking the enemy? 2 Answers