With the transition between Idle and Jump configured in the Animator window, we can script a piece of code to trigger it. With the following recipe, we show how simple it is to control animations through scripting with Unity.
As usual, we follow on from the last recipe. You just need to add a C#
script in the Scripts
folder of the project (as you learned in Chapter 2, Importing 3D Graphics) and name it Char_Animator
.
Access the
Scripts
folder in your Project panel and double-click on the newly created script to open it in Monodevelop, the default Unity script editor.Let's begin by creating an
Animator
type variable to store the reference to the character animator and add the following line at the top of the script:private Animator charAnimator;
Get inside the
Start()
function; here we need to address thecharAnimator
variable we created to the actual animator that we will attach to the character. We do that by adding the following...