- Home /
Animation freezing at first frame
Hi everyone,
I found this script on another thread a while ago and modified it first to return the monster to an idle state if out of range and then I tried to get it to fire an attack animation when enemy is close enough to player. Idle part and run part seem to work fine but attack locks on first frame until player moves out of range.
(the attack is a 35 frame animation if that has any value)
Here's the code, any ideas or leads would be great!!!
var leader : Transform;
var follower : Transform;
var detectRange = 30.0;
var attackRange = 2.0;
var speed : float = 5;
private var character: CharacterController;
function Update(){
if (!character) character = GetComponent(CharacterController);
var dir = leader.position - follower.position;
dir.y = 0;
if (dir.magnitude <= attackRange){
follower.forward = dir;
animation.Play("attack");}
// This little feller right here seems to stop at frame 1,
// moving out of range restarts either idle or run without a problem
// I thought it might be due to the speed that update cycles at but
// run and idle both work fine
// replacing idle with attack has proved that attack animation works well
// (cause someone would ask about that I'm sure)
if (dir.magnitude <= detectRange){
follower.forward = dir; character.SimpleMove(speed*dir);
animation.Play("run");}
if (dir.magnitude > detectRange){
animation.Play("idle");}
}
Your time is appreciated
Hi! You are constantly starting the animation(s) when the conditions are met; if (dir.magnitude <= detectRange){ follower.forward = dir; character.Simple$$anonymous$$ove(speed*dir); animation.Play("run");}
as long as (dir.magnitude <= detectRange) the script will start playing the animation. you need a way to tell if the animation is already playing, and only start it if it is not already playing.
Answer by Montraydavis · Nov 13, 2012 at 01:25 PM
It most likely has to do with the endless animation loop. Try
animation.Play ("AnimationName") ;
yield WaitForSeconds ( animations["AnimationName"].clip.length ) ;
//...rest of code . . . .
Make sure to create another function. The above code should not go inside of Update, but instead, Update should call another function that contains this code. What it does is basically waits for the animation to end before replaying it.
Thanks, moving it out to another function is the way to go!
Your answer