In the previous tutorial, you set up some animation states. You now need to switch between states and you do this with animation transitions.

Transitions perform various functions. They let you know the flow of animations, but, more importantly, you can gate them according to certain conditions. In this tutorial, you’ll creating such a condition when the player steps in a certain part of the arena.

Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.

Setting up transitions

The first part of a transition is a condition, so that’s the next thing you’ll set up. In the Animator window, right-click the ArenaIdle state and select Make Transition.

This shows adding a new transition on the ArenaIdle state

A line will now follow your mouse cursor. Click the WallsLower state to create that new transition.

This shows a new transition.

Note: You might find that a state is out of view. By holding down the middle mouse button, you can pan around inside the Animator and find it.

Create a new transition from WallsLower to WallsRaise, and then another from WallsRaise to ArenaIdle. Your states should look like this:

This shows a transition from three different states

This depicts the entire animation flow. Play your game while watching the animator.

This shows the state in action while the game is played.

Select BobbleArena in the Hierarchy if you don’t see any motion. You’ll notice the animation moves in an endless loop and that some states start before others finish; the Animator is blending the states.

Animation state transition conditions

Looping isn’t ideal in this case; you need a condition to prevent it. Also, blending is helpful, but you might want to make it more or less subtle. In some cases, you may not want it at all.

Go to the Animator window and click the Parameters tab. It will currently read List is Empty because there are no parameters.

This shows the Parameters tab in the animator.

Click the plus sign and select Bool from the drop-down.

This shows adding a new Bool variable.

Give it the name IsLowered. Although you chose Bool in this case, note that the menu has four options to choose from: Float, Int, Bool and Trigger. Except for the Trigger, all of them correspond to the types used in C#.

The trigger is a distinctive Unity condition type that behaves like a bool, except it resets itself after an animation.

Select the transition arrow between ArenaIdle and WallsLower to see your blending options in the Inspector.

Uncheck Has Exit Time to tell Unity you want the animation to conclude immediately instead of waiting until the end of the animation. Otherwise, Unity will take additional time to blend the animation with the next animation.

In the Conditions section, click the plus sign. Since you have only one parameter, the IsLowered property will populate the field; set its condition to true.

This shows the Walls Lower in the inspector

The reason you did this is because you will set the IsLowered boolean in code when you want the animation to trigger. By setting the condition that IsLowered must be true for this animation to run, once you set it in code, the animation will play.

You need to make one more small adjustment before playing the game. In the Animator window, right-click ArenaIdle and elect Set as Layer Default State from the options.

This shows setting a layer as default.

This change indicates that ArenaIdle is the entry state for the animator controller. To make the relationships easier to understand at a glance, organize them like so:

This shows all the states connected

Play your game again while keeping the Animator open. You’ll see the wall doesn’t drop because the condition hasn’t been met. With the game still playing, check IsLowered in the Animator.

Now the wall lowers, raises and loops unless you uncheck IsLowered. You need one more condition.

Stop your game and select the transition arrow from WallsLower to WallsRaise. In the Inspector, uncheck Has Exit Time. Click the plus sign in the Conditions section and set IsLowered to false.

Play it again and click the IsLowered checkbox. This time, the walls will lower, but they stop there due to the new condition. Uncheck IsLowered to raise the walls again.

You now have animations in place. Time to call them in code!

Creating a trigger

At this point, you have animation clips, animation states and conditions in place to activate your animations. Next, you’ll use a collider trigger to put everything together in code.

Select BobbleArena in the Hierarchy then click the Create button. From the drop-down, select Create Empty Child and name it WallTrigger. Set Position to (0, 0, 0) and Scale to (0.5, 0.5, 0.5). Next, click the Add Component button and, from the Physics category, select Box Collider.

In the Box Collider component, check the Is Trigger checkbox, set Center to (0, 6.72, -90.6) and Size to (250.55, 17.14, 66.7).

You should see a large trigger in the Scene view toward the back walls. When the space marine walks into this area, you want to lower the walls.

This shows the box collider setup.

The collider is very faint, but it’s there. Now, to write the code.

Triggering animations in code

Select the WallTrigger then click the Add Component button and select New Script. Name it ArenaWall and click the Create and Add button.

Double-click the new script to edit it, and add this variable inside the class:

private Animator arenaAnimator;

You could make it a public variable so you can set this in the inspector, but this isn’t necessary. This is because WallTrigger is a child of the BobbleArena, so you can get a reference to the animator by accessing the WallTrigger’s parent property, and getting the appropriate component.

To do this, add this to Start():

GameObject arena = transform.parent.gameObject;
arenaAnimator = arena.GetComponent<Animator>();

First, this gets the parent GameObject by accessing the parent property on the transform. Once it has a reference to the arena GameObject, it calls GetComponent() for a reference to the animator.

The above code works because it assumes the caller is a child of BobbleArena. It would fail if the caller were moved to a different GameObject.

Next add the following before the closing brace:

void OnTriggerEnter(Collider other) 
{
  arenaAnimator.SetBool("IsLowered", true);
}

When the trigger is activated, the code sets IsLowered to true. You do this by calling SetBool().

Note: There are similar methods for other types. For example, for a float, you’d use SetFloat().

Now you need logic to raise the walls. Add the following:

void OnTriggerExit(Collider other) 
{
  arenaAnimator.SetBool("IsLowered", false);
}

When the hero leaves the trigger, this code tells the Animator to set IsLowered to false to raise the walls.

Adjusting the collision matrix

There’s one outstanding detail for this script. Currently, any GameObject that enters the trigger lowers the walls. You need a filter so it only applies to the space marine.

Save the file and go back to Unity. With WallTrigger still selected, assign it the Wall layer. Navigate to Edit ▸ Project ▸ Settings ▸ Physics.

Uncheck all the checkboxes in the Wall row. In the Wall column, uncheck Alien Head, Bullet, Column and Floor.

This shows the collision matrix.

Play your game. Just as you intended, the walls lower when the marine walks down the screen, allowing you to see the action. How’s that for convenience?

Where to go from here

Congratulations! You build your first state machine. Granted, the use case was a little contrived, but it should demonstrate the flexibility of Unity’s mecanim system in code.

So far, you’ve created your own animation and you’ve called it in code. Your next step is to use animations imported along with models. You’ll do this in the next tutorial.


Discover more from Jezner Blog

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply