Player Setup Part 4

Simon Leen
3 min readSep 19, 2021

Objective: Create Transitions between Animations.

Notice above we have two animations but the Player isnt moving with the run animation. They also have to be triggered individually in the animation window. To get these to work properly and transition from idle to run when the Player moves and back to idle when the Player stops, we need to use the Animator Controller.

To create the transition we create a float parameter called speed. For the transition conditions we transition from idle to run when speed is greater than 0 and from run to idle when speed is less than 0.1. On the transitions we turn off exit time to avoid delays between transitions as leaving this on the anaimtion would have to finish first before transitioning.

Next we create a PlayerAnimation script and attach it to the Player. We create a private Animator and assign it in the Start method using GetComponentInChildren as the Animator component is on the child sprite object and not the Player object itself. We then create a public Move method that takes in a float and set the animator parameter speed to the Mathf.Abs of that move float. We do this as when the Player is running backward the value would be in minus, therefore staying in idle instead of running. Using the absolute value gives us a 0 or 1 result. We use public as this method will be called from the Player script.

Back in the Player script we create a private PlayerAnimation and assign it in the Start method(This gives us access to the PlayerAnimation script). We then call the Move method in the Movement method after gettting our float value horizontal(hori) and pass the value through to the PlayerAnimation script which sets the value for the transitions.

We now have transitions between idle and run based on Player input.

In the next post, we will fix the rotation of the Player when running to the left.

--

--