Day 75: Using the Unity Animation System

Tyler Smallwood
5 min readAug 10, 2021

--

Objective: Use Unity’s Animation system to get the Object look like it’s walking.

Let’s set up the animation system to have our player actually walk!

For this project the animations for walking and idle have been created already, we just need to set up the logic.

We will start by adding a animator component on our player object.

Now we need to create a controller, in our project files we have our animations. In the same folder we will create our controller. To do this right click in the folder go to Create -> Animator Controller. I named mine Darren, the characters name.

We need to the controller to the players Animator component. We do this by dragging the controller into the slot on the animator.

Now if you play the game nothing will happen, this is due to the animations not being apart of the controller. Let’s add them and the transitions! We need to open the Animator window now, this can be done by double clicking the Controller or by going to Windows -> Animations -> Animator.

The animator window should look similar to what is shown below, the Exit, Entry, and Any State states may be arranged different.

In our Animation folder we can drag the walk and Idle animation into this window. The first animation brought into the window will turn orange. Orange signifies the Default State. This will automatically play when the game is started. To change a different state to default, just right click on the correct state and click Set as Layer Default.

Let’s make some transitions! We will want to transition from Idle to Walk animation when the player clicks in the game. To do this we need to know if we are walking or not, so in the left panel of the Animator window, we’ll create a bool named walk.

Next will be set the transition lines up to only transition when the Walk bool is true. Right click on the Idle state and click Make Transition.

Then click on the Walk state, you’ll get a white line from Idle to Walk. Repeat this from the Walk to Idle.

Click on the Transition line going from Idle to Walk. We will set the condition up and make sure Has Exit Time is unchecked. Has Exit Time, when check, will let the current animation finish it’s animation before moving on. Having it uncheck will start to transition as soon as the condition is met. We want the condition to be Walk is equal to true to start the walk animation.

To move back to Idle we want the condition to be Walk is equal to false.

We need to set up the Code logic to get the animation working. To get the code to work with the Animator, we need a variable of type Animator. After we get the variable we need to set the bool at the correct time. To start, lets get our variable and use GetComponentInChildren to get the Animator.

I am using GetComponentInChildren because the Player script is on how Player Object and our Animator is on the 3D Model object that is a child to the Player object.

Player Object with the Script.
3D model with the Animator

Next is to set the Walk bool to true when moving and false when the player stopped. To start with true. When we check for Input from the mouse and we hit the floor, we use the variable name, in this case _anim, and SetBool(“Name set in the Animator Window”, true). This will allow the player’s character to look as if it is moving.

Get the the animation to stop we need to set the bool to false.One way of this can be done by checking to see if the player’s position is equal to the location of the click To do this we need a new Vector3 variable to save the hit.point from clicking. As seen above we named it _hitLocation and saved the hit.point to it. To check to see if we are equal we will check if this objects transform.position.x & .z is equal to the _hitlocation.x & z. I used x and z because the y location is set higher than the players y position.

--

--

Tyler Smallwood
Tyler Smallwood

Written by Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

No responses yet