Adding and Animating a Character
Now that we have our project upgraded to URP, let’s add in a character model to work with and start getting the animations going for it. To start, we will create an empty object as a child of the capsule object we are using, and attach our model to that object, along with deleting the mesh renderer of our capsule so it is no longer visible, but can still interact with our game:
Sometimes when adding in a new model, we get left with a magenta coloured figure. In order to fix this, we just have to run an upgrade for our materials:
From here, we will be left with just a glowing hairstyle, which we will take care of by going into the model and changing a setting to have it run through URP:
Now that we have our character in place, let’s adjust our capsules collider so that it encompasses the player’s figure:
With all of this done, let’s see how it looks within our game:
As we can see, it runs around like we want, but we need to work on animating the movements so that it doesn’t just look like figure just sliding everywhere.
First, let’s head over to Mixamo and we can pull from a variety of character animations there for free. From there, we will download whichever Idle animation we want and drag it into our project view. As we are working with a humanoid figure, we will want to be sure to change the rig settings to humanoid:
From here, we will just duplicate the animation so that we can adjust some of the settings for it:
From here, we just drag our animation to the controller that we need for the character and play it in game to see if it works:
Now that we have a working idle animation, let’s work on getting a running animation in place. When we grab our animation from our site, we will want to be sure that we check off the “In Place” option:
This will have it so that our animation does not control our characters momentum. From here, we drag it in and apply it to our animation controller like we did with our idle animation, but now we create a transition to our running motion, along with going back to idle. In this case, we will state the speed determines if we are running or idle:
Now in order for us to make use of this parameter, we need to code it in so that when change our state, we change our animations too:
However, just doing this will only work in 1 direction, as we are telling Unity that if speed is greater than 0.1, we want to animation to run:
When we are moving backwards, we are moving in a direction that is lower than 0, so therefore we will have it grab the absolute value of our variable instead:
Now, let’s see how this looks:
We have our animation working both forward and backward, however now our model is turning towards the camera while running. This is a quick fix, as all we need to do in this situation is turn off our root motion:
Now that all of this is done, let’s see how it looks:
Now that we have finished up creating some idle and running animations, we can look at how to build in jumping along with how we can flip our character around so it doesn’t give it the awkward look of sliding backwards.