Join Jesse Freeman for an in-depth discussion in this video Managing player animation states, part of Advanced Unity 2D: Platformer Player Movement.
- So at this point, we're now able to make the player face a particular direction and to walk. But we're going to need to connect up the animations. In order to do this, we're going to need to create a class that is actually tightly coupled to each of our behaviors. What that means, is that we'll need one manager class that will know the state of each of the behaviors and be able to update the display accordingly. This is the opposite of what our behaviors are, which are loosely coupled. They know nothing about each other, they only share a common property, which is a reference to the input manager.
In order to get started, let's go into our script folder, create a new folder, and we're going to call this Managers. In here we'll create a new C# Script, and we're going to call this, Player Manager. Let's open this up in MonoDevelop. Now, for this to work, we're going to need a reference to the input state and the actual walk behavior. So let's create two private properties to do that. Next, we'll need to create a new awake method in order to set these two properties.
Here, we can copy the input state, paste it down, and replace it with the walk behavior property and the walk component. Now before we move forward, we're going to need a few values in the input state, in order to let us know what's actually going on with the rigid body and its velocity. Let's go into our input folder here and open the input state. Now we'll need to add three new properties. Two of them are going to manage the velocity X and Y values. And the other is going to be a reference to the rigid body2D.
Once we have our absolute value X we can copy this and make it Y. And then we'll create a private property for the rigid body2d called body2d. Next, we'll need a reference to this body2d component. So let's create a private awake method. And here we'll simply set the value of body2d to its component. Next, we'll want to calculate the absolute value X and Y position from the rigid body's velocity.
In order to do that, we're going to take advantage of a special method that's reserved for making physics calculations. This is going to be called, fixed update. Unlike the regular update, fixed update is called at a limited number of intervals during the update loop. This is important, because we don't need to always calculate the physics constantly at 60 frames a second. Here, we're gonna set the value of absolute value X and Y to its corresponding velocity. We'll use Unity's Math Library, select ABS in order to get the absolute value and then look at the body2d velocity X.
Then we can duplicate this line and change this to Y. I use the absolute value of X and Y a lot throughout my games, in this case, I don't necessarily need to know whether the body is moving to the left to the right, whether it's a negative value or a positive value, I just want to know if there's actually motion in place. So in this case, by simply getting the absolute value I will know whether it's greater than zero or if it's at rest, which would equal zero. Let's go back into our player manager and connect this up.
First, we'll need to create a new method that'll allow us to change the state of the animation based on the absolute value that we detect. Here, we'll make a new private method called change animation state. And we'll pass in a value that's tight to int. Before we actually change the state, we're going to need reference to one more component. In this case, the animator on our game object. At the top we'll create this property for animator. Here, we can duplicate how we get the values of the other components and simply paste in the animator property and get the animator class.
Now, we'll be able to tell the animator to actually change its value. In this case, we're going to set an integer to a property called AnimState and we'll pass in the value that we supply the change animation state method. Now inside of update, we'll want to calculate what the absolute value of X is doing and change the animation state accordingly. In the first condition, we're gonna look at the input state and check its absolute value X to see if it equals zero.
If it does, we're going to change the animation and we'll set its value to zero. Next, we can copy this condition, paste it below and change what we're testing for. In this case, we want to see if the absolute value is greater than zero. If it is, that means that the body is in motion and we can change its state to one. This will wind up being our walk animation. At this point, everything should be wired up. The only thing left to do, is for us to connect up our animations.
- Building an input manager for keyboard and controllers
- Setting up the player
- Building reusable, self contained, and modular behaviors scripts
- Adding behaviors: walking, running, and jumping
- Managing animation states and player collisions
- Adding a ducking animation
- Detecting wall collisions
- Sticking to, sliding down, and jumping off walls
- Picking up items
- Firing projectiles