Learning Unity — More Advanced Animation Practices
In the last article, we learned how to switch between Unity scenes using scripting. In this article, we are returning to the topic of animation to learn more advanced techniques.
The last time we discussed the topic of animation in Unity, we created an animation that was fairly basic. It simply ran immediately upon starting the game without any sort of input from the user.
But most animations in a game won’t operate in that way. Typically, it’s something happening in-game that causes an animation to be triggered. Let’s see how we can accomplish that.
The first thing we will need to do is create our animation. Check out my earlier article on the subject of animation to see how to do that. However, there will be one (potential) difference in how we want to do things this time compared to that earlier example.
In the previous article, the example I created was just of an explosion, so the base sprite I created for the game object was virtually invisible. This time around, we will eventually have an animation that doesn’t trigger immediately, so we will want a static sprite that is visible from the start.
Aside from that difference, the animation can be created through following the steps in the article. Once finished, we should have what I described earlier — a game object with an animation that plays immediately upon starting the game.
To add some in-game logic to our animation, we need to open the Animator window in the Unity Editor. There we should see something of a diagram.
Here we can see the state machine of the animator. A state machine is a model that represents the different states something can be in, the transitions between those states, and also what causes transitions to occur.
In this context, it represents how the animator manages the different animations that are assigned to it. For right now, we are only interested in the “Entry” state and the “Animation” state.
The “Entry” state represents the entry point into the state machine. It is not an animation state itself, but instead allows us to start with any of a number of different animations or no animation at all.
The “Animation” state is the state where an animation is presently playing. It will actually have the same name as the specific animation assigned to the state by default, though it could be renamed at will.
In order to prevent the animation from running immediately, we need to add a new default state to the state machine. Right-click in the window and select Create State → Empty. Then, once it is created, right-click on the new state and select Set as Layer Default State.
If we run the game now, the sprite will remain static and the animation will never play. We can fix this by right-clicking once again on the new empty state and selecting Make Transition. We should then see an arrow coming from the empty state, similar to the arrow going from the entry state to the empty state. Click on the animation state to connect the arrow between them.
If we run the game again, we’ll be back to the animation playing automatically, but now with a slight delay. This is due to the Exit Time setting of the transition. Feel free to play around with those settings to check out what they can do.
The next step in making our animation run when we want it to run is to create a parameter in the Animator window. Make sure the Parameters tab is selected, then find the plus sign next to the search bar. When that is clicked, we see a list of four options — Float, Int, Bool, and Trigger. We want to create a Trigger.
Next, on the transition between the empty and animation states, we need to find the Conditions section. It will probably say “List is Empty”. Click on the plus sign below it to create a condition. If there are multiple parameters, find the appropriate one in the dropdown menu of the new condition.
At this point, we’re nearly there. If we run the game now, the sprite is still static and the animation does not run. However, if we open our Animator window while the game is running and click the circle next to our trigger, the animation does run. Now it’s just a matter of activating that trigger without having to do it through the Unity Editor.
Our last step is to create a script to handle this. Create a script, attach it to the game object, and then open it in an editor.
For this script, we really just need three things:
- We need a triggering event. In this example, I have made the space key being pressed activate the trigger.
- We need access to the Animator component. In this example, I use the GetComponent method to access the game object’s Animator.
- We need to activate our trigger. This is accomplished by using the SetTrigger method of the Animator. The parameter for that method is the name of the trigger parameter.
Now when we run the game one last time, we can see that our sprite will remain static and unchanged until we press the space key, at which point our animation will run.
All right, we did it! That was a long one, but we just picked up some valuable knowledge on how to breathe life into our games. In the next article, we’re going to dive even further into the subject of animation by learning how we can bring together static sprites and animations to make a more interesting whole. Until then, good luck and happy coding!