- Home /
Enemy Animation Freezing in first frame
Hi I am trying to get an enemy to play 2 animations
the first when he is not moving and the second when he runs at the player. currently the enemy will play the idle animation but only play about 2 frames of the run animation.
Any assistance on this would be greatly appreciated
var distance;
var target : Transform;
var lookAtDistance = 10.0;
var attackRange = 6.0;
var moveSpeed = 5.0;
var damping = 6.0;
animation["idle"].layer = -1;
animation["run"].layer = 1;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
animation.Play("idle", PlayMode.StopAll);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
animation.Play("run", PlayMode.StopAll);
}
Answer by Lockstep · Sep 16, 2012 at 01:37 AM
The problem is this line of code you are calling during Update() animation.Play("idle", PlayMode.StopAll); It stopps all other animations and plays idle every frame. Delete that line and try this instead
function Start(){
animation.CrossFade("idle", PlayMode.StopAll);
}
//inside Update
if(distance < attackRange)
{
attack ();
}
else
{
animation.CrossFade("idle", PlayMode.StopAll);
}
function attack()
{
//other stuff
animation.CrossFade("run", PlayMode.StopAll);
}
CrossFade is usually nicer to look at because it fades the animations into eachother in a smooth way. See http://docs.unity3d.com/Documentation/ScriptReference/Animation.CrossFade.html . Also make sure the wrapmode is set to loop.
Your answer
Follow this Question
Related Questions
play animation when moving,and idle animation when stand still 1 Answer
Enemy AI Animation 2 Answers
Using multiple buttons in script 3 Answers
Slow down and play walk animation on left shift? 3 Answers
Animay play in editor 0 Answers