- Home /
Enemy Jitters when i want him to stop a certain distance
i want the enemy to walk towards you once he sees you. after he comes in a certain distance I want him to stop moving. problems is that he is still moving within the decimal. This gives the enemy that strange jitter effect because he keeps bouncing between decimals.
function PlayerDetected()
{
Debug.DrawLine(target.position, myPosition.position, Color.red);
myPosition.rotation = Quaternion.Slerp(myPosition.rotation, Quaternion.LookRotation(target.position - myPosition.position), rotationSpeed * Time.deltaTime);
Debug.Log(Vector3.Distance(target.position, myPosition.position));
if(Mathf.Round(Vector3.Distance(target.position, myPosition.position)) < closeInDistance)
{
StopWalkingAnima ();
}
if(Mathf.Round(Vector3.Distance(target.position, myPosition.position)) >= closeInDistance)
{
myPosition.position += myPosition.forward * moveSpeed * Time.deltaTime;
LockRotation();
enemyAnima.EnemyMove(moveSpeed);
enemyStandOnce = true; //to stop the walking animation when you are out of sight
}
if(Vector3.Distance(target.position, myPosition.position) < wayToNearDistance)
{
myPosition.position -= myPosition.forward * moveSpeed * Time.deltaTime;
LockRotation();
enemyAnima.EnemyMove(moveSpeed);
enemyStandOnce = true; //to stop the walking animation when you are out of sight
}
}
Comment
found the problem, i only keeped my lock rotations in the If statements not the whole function. works good now
Your answer
Follow this Question
Related Questions
Distance won't work 1 Answer
How to move Instantiated object to pre-defined position(Solved) 1 Answer
Vector3.Slerp not working 2 Answers
Find position of point beetween two objects on certain distance 1 Answer
Vector3.lerp doesn't work 1 Answer