Playground Pattern: Player Animation

This tutorial will walk you through setting up your animations for your player using the Unity Playground’s MoveAnimation script. This script is only available in my updated version of the Unity Playground, NOT in the official download from Unity.

You should already have your animations created prior to starting this tutorial.

Previous Tutorials

Step 1 – Create an Animator Override Controller

An Animator Controller is essentially a big flow chart that helps Unity decide what animation to use on a GameObject at any given time. You can create these yourself, but if you are using my Playground update, you don’t have to. Instead, I have set up a controller to work with common player and enemy movement, and all you have to do is override it.

An Animator Override Controller takes an Animator Controller and allows you to replace each animation with your own, without having to set up the controller yourself. This makes the whole process a lot easier for beginners, and is also really useful for experienced Unity developers for making a lot of creatures / players with similar animations.

To create one, first go to your animation folder. Right-click in the folder and choose Create > Animator Override Controller. Name your controller something useful like “Player Animator”.

Step 2 – Select your new Animator Override Controller

Step 3 – Set the Controller which will be overridden to the Player Movement controller in the _Internal_ > Animations folder

We need to tell our new Override Controller which base contorller it will be overriding – the controller which is already set up for you in the Playground project. To do this, click the circle next to the “Controller” field in the inspector for your new Override Controller, and select the “Player Movement” controller which is in the _Internal_ > Animations folder.

Step 4 – Add your animations to the Override Controller

You will now see a list of animations which you can override, corresponding to different situations in the game. If you don’t override them, no animation will play in those situations.

The situations are as follows:

  • Action 1 – A custom action (such as attacking) that can be triggered manually by the AnimationAction script
  • Action 2 – A custom action (such as attacking) that can be triggered manually by the AnimationAction script
  • Action 3 – A custom action (such as attacking) that can be triggered manually by the AnimationAction script
  • Action 4 – A custom action (such as attacking) that can be triggered manually by the AnimationAction script
  • Falling – In platformer style games, used when the GameObject is not touching the ground
  • WalkDown – Played when the GameObject is moving downwards
  • WalkLeft – Played when the GameObject is moving to the left
  • WalkUp – Played when the GameObject is moving upwards
  • WalkRight – Played when the GameObject is moving to the right

To make your animation play for any of these situations, just click the circle next to that situation and select your animation, or drag your animation into the slot.

Step 5 – Select your Player’s GameObject

Make sure you have already set up your player’s movement using one of the Movement scripts.

Step 6 – Drag your new Animator Override Controller into the player’s Animator component, to the Controller field

Step 7 – Add the MoveAnimation component to your Player

The MoveAnimation component is what will choose the correct animation to play when you player moves or jumps. This is part of my extended version of the Unity Playground, and is designed to work with the Animator Controller that we just used in our Override Controller.

Step 8 – Update the MoveAnimation settings to fit your Player

There are a number of settings on the Move Animation script.

  • Movement Type – Change this based on if the movement should include all four directions, or only left and right or up and down.
  • Object To Animate – Only change this if the thing you are animating is not the same object where the MoveAnimation is attached. If you followed this tutorial, you should leave this as “None (Animator)”.
  • Flip Move Right – Tick this box if you should flip the sprite to face the opposite way when moving to the right. Use this if your animation is set up to be facing left by default.
  • Flip Move Left – Tick this box if you should flip the sprite to face the opposite way when moving to the right. Use this if your animation is set up to be facing right by default.
  • Use Fall Animation – If you are making a platformer and have a jump / fall animation, tick this box.

Step 9 – Test your animations

Test these animations out in your game using the Unity Editor’s play button. Adjust settings as needed.

More advanced animations such as attacking can be triggered using the AnimationAction script – this will be covered in a later tutorial.

Further Reading

  • Playground Scripts:
    • AnimationAction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s