I would imagine this is because your movement code is just inside your update, meaning the movement code is not bound to any “too close / too far” logic at all. Try something like this.
function Update ()
{
// Rotate to face player.
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) <= AttackDistance);
{
// Player within range. damage player.
PlayerHealth = PlayerHealth - 0.000001;
print(PlayerHealth);
// Only need to check player health here as this is where it is decreased.
if(PlayerHealth <= 0)
{
Application.LoadLevel(1);
}
}
else
{
// Too far away, move towards player.
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
}
I’ve added an else to your distance check so that if the player is not within range that will then trigger the movement logic. Additionally I moved the health check to where the health is decreased. It’s a minor optimization so feel free to ignore that part, the important part is the else, and the movement of the transform.position change into it.