- Home /
Enemy Jitters When Following
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?
function Update(){
//FIND THE PLAYER & CHANGE STANCES//
if(!FindPlayer){
Player = GameObject.FindWithTag("Player");
FindPlayer = true;
}
var distanceToTarget = Vector3.Distance(transform.position, Player.transform.position);
if(distanceToTarget <= 3){
targeted = false;
}
if(distanceToTarget <= 15 && distanceToTarget > 3){
StanceChange = gameObject.GetComponent("EnemyControl");
StanceChange.BattleStance = true;
targeted = true;
}
if(distanceToTarget > 15){
StanceChange = gameObject.GetComponent("EnemyControl");
StanceChange.BattleStance = false;
}
//LOOK AT THE PLAYER ONCE TARGETED//
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");
//CHASE THE PLAYER//
animation.CrossFade("run",0.2);
var controller2 : CharacterController = GetComponent(CharacterController);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
controller2.SimpleMove(forward * walkSpeed);
}
}
You might want to set some Debug.Log messages inside your loops to check if your values are actually being set to what they need to be.
If I have to guess though (and assu$$anonymous$$g that no other script is interfering with that script), I'd have to say that your enemy never gets within stopping range of the player and that your Quaternion.Slerp is at fault. You see, you're slerping using Time.deltaTime, which means that unless your damping variable has a relatively high value, then your enemy never actually gets to face the exact direction as your player. It would always stop just a small angle from the actual direction vector and would keep readjusting itself as long as it's being called. Couple that with the forward movement that's being called afterwards and you're possibly causing a scenario where the enemy is moving in a small circular motion near the range while not reaching the range while continuously slerping towards the ever-changing target rotation.
Anyway, while my theory may seem plausible, it can't be verified unless you actually set Debug.Log messages like I suggested.
@Jordan 9: I have removed my answer, since you seem to be more keen on voteing down than getting my help. BYE!
Your above code is fine (ignoring inefficiencies), so you need to look and show us elsewhere.
Well I think that it's switching the targeted variable on and off but it should just stay off, if that helps at all :S.
I tried your code and it does what it's supposed to do. The enemy, assu$$anonymous$$g that the required values are all tweaked to scale, is able to to get to within stopping range and stops when it does. No values are fluctuating or anything, so the problem really is in some other script.
Your answer

Follow this Question
Related Questions
Enemy backing away from player after going around corner? 1 Answer
Enemy is not stopping following the player 2 Answers
Enemy reorienting for player 1 Answer
Camera rotation around player while following. 6 Answers
Enemy to follow the Player 1 Answer