- Home /
Enemy vibrates when it reaches the player
Hello programmers around the world i am having a problem with an enemy Script which controls my enemy. The original idea was that if the player gets in range of the enemy, the enemy starts to move towards him and when it reaches the range where it is supposed to stop it does. However it doesn't at the same time because for some reason it just twitches and vibrates just in front of the character. Any help any ideas because I can't solve this other than that my script works perfectly! Thanks in advance!
private void FollowTarget()
{
if (target != null && player.inLineOfSight)
{
StopCoroutine(EnemyIdle());
float distance = Vector3.Distance(player.transform.position, enemy.transform.position);
if (distance > enemy.MyAttackRange)
{
Vector3 targetDir = player.transform.position - transform.position;
float step = runSpeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
transform.rotation = Quaternion.LookRotation(newDir);
transform.position = Vector3.MoveTowards(transform.position, target.position, runSpeed * Time.deltaTime);
if (distance == enemy.MyAttackRange)
{
eAnim.SetBool("walk", false);
rBody.velocity = Vector3.zero;
}
}
if (distance <= enemy.MyAttackRange)
{
if (player != null)
{
if (autoAttackCurrentTime < autoAttackCoolDown)
{
autoAttackCurrentTime += Time.deltaTime;
}
else
{
DoDamage(15);
player.GetComponent<Player>().health.CurrnetValue -= 15;
autoAttackCurrentTime = 0;
}
}
}
}
}
Calling the FollowTarget() function in update where the target is set to be the character controllable by the player.
Comment
Sorry for the bad formatting i don't know why it came out like that.