- Home /
Playing several animations
Im having problems, my walk and idle work fine but when i press my button to call an attack script it plays a second but cuts off the rest of the animation.
if ( moveTouchPad.position.x > 0)
transform.rotation = Quaternion.Euler(0,90,0);
animation.CrossFade("walk");
if ( moveTouchPad.position.x < 0)
transform.rotation = Quaternion.Euler(0,-90,0);
animation.CrossFade("walk");
if ( moveTouchPad.position.x == 0 && attack == true)
animation.CrossFade("idle");
function OnGUI() {
if (GUI.Button(Rect(10,Screen.height / 2,50,50),btnTexture))
{
animation.Play("attack");
}
}
I think you need to check attack == true for the walk animations. You're also calling CrossFade("walk") in the main loop, forget some brackets? I think you need to put the transform and CrossFade calls in brackets.
Answer by RyanZimmerman87 · Feb 02, 2014 at 02:24 AM
First of all you should probably remove the:
animation.Play("Attack") from OnGUI()
I don't think it's necessary to do so but that seems like an odd way to do things, I would probably set up an attack bool which turns true on button press and turns false once the attack completes. And you could add the bool logic to your button press so it only works if your attack is complete.
For example if you press button in OnGUI and it turns attackBool = true;
bool attackBool;
bool attackActiveBool;
void Start()
{
//set animation Wrap Mode for the attack to "Once"
animation["Attack"].wrapMode = WrapMode.Once;
}
void Update()
{
//after OnGUI() button press this will be true
if (attackBool == true)
{
//start attack animation
if (attackActiveBool == false)
{
gameObject.animation.Play("Attack");
attackActiveBool = true;
}
//once the attack animation completes
if(!animation.IsPlaying("Attack"))
{
attackBool = false;
attackActiveBool = false;
}
}
}
But for your specific problem I beleive Crossfade is messing things up for you. If you want to use an animation and gaurantee it plays the whole thing and looks correct visually you should probably use animation.Play() instead of animation.CrossFade().
I have always had trouble using Crossfade for anything important. While it may look much better the logic gets a little goofy especially if you are using Animation Events.
Maybe I don't fully understand it but I feel CrossFade needs better documentation or some game engine improvements to use it correctly for important game play type stuff.
You may need additional logic to ensure no other animations can possibly play when attackBool == true
Your answer
Follow this Question
Related Questions
Animation Stop and Stay 1 Answer
Animation problem in script 0 Answers
Walking animation has wrong rotation 0 Answers
Zooming animation doesn't work 1 Answer
Why is Animation Not Playing in Reverse? 2 Answers