Activate and Deactivate Animations With a Button Press

Hi

I have a variety of animations which I wish to play with a button press

I want a ‘jet pack’ animation to play and then a ‘hover’ animation on a button press and then a ‘deactivate jet pack’ animation on the same button press.

This is the script I have so far:

var activated = false;

function Update()
{
if (Input.GetButton(“jetpack”))
{
activated = !activated;
if (activated)
animation.Crossfade(“jetpackActivate”);
else
animation.CrossFade(“jetpackDe-activate”)
}
}

However, it seems to be a little sketchy and doesn’t always work!

Any help would be amazing!

Thank You

Off the top of my head, I think you could put the code which activates and deactivates the jetpack in its own function, called from the key press under Update, so you can put a slight delay on it with yield, or some other way. This is so you have time to let off the button so the variable doesn’t immediately flip back. I’ve run into what I think might be similar problems and this solved it.