Using the Unity Animation System
Now that we have our movement working along with the camera following around let’s switch over our capsule placeholder to our actual sprite and get the animation for him moving around working. To start, we are just going to drag in our sprite to be a child of our player capsule and turn off the mesh renderer for the capsule. From there, we want to increase the collider and nav mesh area to encompass our new player sprite:
Now that we have him in the game, the next step is to have his animations work so that it isn’t just a chess piece sliding across the floor. First, we will need to create an animator controller within the folder of the animations for that particular object:
From here, we will open up our animator controller and be brought to an animator window in which we will be connecting our various animations to specific commands:
In order for our objects to use these animations, we need to attach an animator component to them and drag over the controller to the designated slot:
Finally, before setting up the circumstances for the various animations, we can drag in the different animations that we will be using into the window and connecting to the initial state of our playable character:
From here, we are going to create a transition to our walk state, as well as from walk back to idle:
Since we do not want our animations to keep going when they reach their end point, we will need to make sure we turn off the exit time so that once our player stops, he doesn’t just walk in place for the rest of the animation:
Finally, we need to create a bool to work with for our player that we can manipulate to let unity know when we want our player to transition to the other animation:
Before we create our method to transition, let’s see if clicking the statement on and off will transition between the 2 states:
To create this logic, we need to head into our player script and create new code for it:
First off, we need to create logic for our animator so that we can interact with the parameter we set for it. As our sprite is a child of the player object we are using, we will have to be sure to pull it as a child.
Next, we need to create a bool for our walk to switch between true and false. From here, we will also need a new Vector3 so that we can call upon out hitInfo.point outside of the if statement we already have in place.
Finally, we will set up our logic to calculate the distance between the player and the end point and tell Unity that when it is a value lower than 1, we want to turn off the walking animation:
If we felt like our player was sliding across the floor a bit too much, we can adjust a few of the settings within our Nav Mesh Agent in the Unity inspector:
Now that we have our player’s animations all set up, we can look towards working on the next portion of our game.