O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - animation interrupts

The trouble is that we're telling the character to play his "catch" animation, but it's not happening. We see, at best, the first frame of that animation, and then it's interrupted by either the "idle" or "step" animation.

Luckily, we can add a condition to the script to prevent these glory-hog animations from playing if we're trying to catch a beer stein. Dip back into your CharacterScript and make these changes:

if(lastX != transform.position.x) {
if(!isMoving) {
isMoving = true;
if(!animation.IsPlaying("catch")){
animation.Play("step");
}
}
}
else {
if(isMoving) {
isMoving = false;
if(!animation.IsPlaying("catch")) {
animation.Play("idle");
}
}
}

By wrapping the animation.Play calls in these animation.isPlaying ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required