Hello i have a script that is attached to a deer in my scene, it does so the deer run away from the player when hes getting close. But i want the deer to play an animation when he runs, So i made an animation and it works fine. But when the deer is outside of the distance to run, then the animation still runs. How do i fix this? (Make so the animation dont play when the deer is outside of the run distance)
Code:
var Player : Transform;
var AnimalMovementPoint : Transform;
var Distance;
var RunDistance = 15.0;
var Damping = 6.0;
var movementSpeed = 4.0;
function Update ()
{
Distance = Vector3.Distance(Player.position, transform.position);
if(Distance < RunDistance)
{
RunFromPlayer();
}
}
function RunFromPlayer ()
{
GetComponent.<Animation>().Play("Run_Deer");
var rotation = Quaternion.LookRotation(AnimalMovementPoint.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
}