Hi, I have a script where an enemy is supposed to start a running animation when the player is in range, and move towards the player. I’ve got it so far that when I approach the enemy the running cycle works fine, and I get an error “Animation ‘enemyrun’ not found”, even though it’s clearly playing enemyrun. This means the rest of the script doesn’t work because it stops at the error, meaning the enemy never actually goes towards you, however stays in one place playing the run animation, and rotating to look at the player when the player moves.
I’ve set the animation type to legacy which got it to actually play, but I’m still getting the error. Here’s the code-
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 110;
var TheDamage = 17;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
GameObject.Find("hmmm").audio.Play();
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
GameObject.Find("outsider").audio.Play();
animation.Play("enemyrun");
moveDirection = transform.forward;
moveDirection *= moveSpeed;
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
animation.Play("enemyattack");
Target.SendMessage("ApplyDamage", TheDamage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDamage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
thanks!