Another important topic in this book is how to animate Jake. Certain animations will be played on the character for specific events. You probably noticed Jake's face is sad during the game. Unity has a really clever system already built-in for controlling animation: Mecanim. If you haven't heard about it yet, I encourage you to dive into Unity's documentation. Anyway, we won't go into too much detail about animating stuff in Unity. All I want you to know is that I have prepared the following animation clips for you:
To preview how animation clips are connected, open the Animator view by going to Window | Animator and selecting the Player game object from the hierarchy:
This is an Animator view. As we want to focus on programming in this book, we won't discuss the details here. All I want you to notice is the Parameters tab and two bool
type parameters we have already set up. To control animations on Jake, we just need to make sure IsGrounded
and isAlive
bools are set to the correct values. We will do that from our PlayerController
script.
The first step is to access the Animator component in the code. We already learned about the GetComponent
method of Unity. This time, I want to talk about another way to gain easy access to certain components. Add the following line to your code just after the line that declares the rigidBody
variable:
public Animator animator;
Save the script, select the Player game object, and look at the Inspector panel. We just created a new public member of the Animator type and called it animator
. The Animator field will appear in the Inspector window. The next step is to assign this variable. Have a look at the hierarchy. There is a child object of the Player game object called sprite
we have the Animator component on. Simply drag the sprite
game object from the hierarchy on top of the
None (Animator) field. Unity will automatically recognize that you are trying to assign Animator from the sprite
game object to the Animator variable:
After a correct assignment, the Inspector window will look like the preceding screenshot. Now, we have really easy access to the Animator component through the animator
variable within the PlayerController
script. Let's put that to use. First, set the isAlive
animator parameter to true
, as we want Jake to be happy at the start. Add the Start
method in PlayerController
with the following line:
void Start() { animator.SetBool("isAlive", true);
At the end of the Update
method, inset this line to set the IsGrounded
animator parameter:
animator.SetBool("isGrounded", IsGrounded());
Save the code and press Play in Unity. Jake should be set to the alive state at the start and change his face a little during the jumps. This is cool, isn't it?
Jake is very much alive now—smiling, jumping, and running... in the same place. The next task is to make him run forward. You probably know how we are going to approach this. We can add a constant force forward. Add a runningSpeed
float public variable to the PlayerController
class:
publicfloat runningSpeed = 1.5f;
Also, add the following method to apply the force:
The FixedUpdate
method is called automatically in Unity after a fixed time interval; it is not like the Update
method, which is called every frame.
FixedUpdate
is the best place to add constant forces to your physics. If you wish to learn more about the FixedUpdate
method, dive into Unity's documentation. For now, all you need to understand is that this method is called all the time, and you do not need to call this yourself. In line 34, we are applying a Vector2
velocity to the rigidbody
. The new Vector2
is a simple C# constructor, and we are passing the x
and y
values in brackets. Our character is moving from left to right, so we are applying force with x
equal to the running speed and leaving velocity y
unchanged.
Line 31 prevents constant acceleration. We are only applying force if the x
velocity is lower than the running speed. In human words, if the character's speed is slower than the running speed, push him forward.
Go ahead, save your script and press Play in Unity. You will see Jake is moving forward now. We have implemented running and jumping functionality. I think we can easily call it our first gameplay! As you probably noticed, Jake will run forward until he falls down from the platform and will be falling forever until you restart. We are missing a game-over event here. In the next chapter, we will write the GameManager
class to help us clamp the whole game functionality together.