There are a lot of different ways that you can get your character to move around but what is the most efficient and simplest way to get your character moving without too much code? Well, we can get the animation and movement out of the way in one simple go! By grabbing our current position and letting the animator know when we are moving, we can combine the two into both moving our character and animating it with a few simple lines of code. You can follow this written out guide and refer to the step photos attached at the top if needed.
If you need the animations we use, you can download them off Mixamo or download our provided link which is a more organized folder of animations that we downloaded from Mixamo. (We had an issue importing Mixamo animations, make sure to import “with skin” if your downloading animations off Mixamo)
The first step into starting our game is obviously creating a character, here we are going to simply add in a rigged and animated model with an Animator component attached. All we need is a blend tree set up with our idle, walk strafe & run strafe, equaling a total of 9 animations. Within our blend tree, we set our walk Animations to “0.5” and “-0.5”, while with our running animations we set those to “1” and “-1”.
Inside of our parameters within our Animator, we will add two floats, “VelX” and “VelY” to get our Horizontal and Vertical positions.
Now we need to create a C# script to tell our Animator our current X and Y position, so create a new script and title it “playerController”. At the top of our script we are going to set a variable “Public float maxSpeed;” and “Public Animator anim;”. Inside your Update void we are going to place two lines of code that will grab our players horizontal and vertical position:
Now we finish off our script by updating our in-game position. We create a new void called “Move” and within that void we update transform.position, multiply it by our maxSpeed float and our current X or Y.
Now we add our “Move()” command into our update underneath our var X & Y so that it looks like this:
Attach this script to your gameObject, make sure the Animator is attached and set up and press play, your character should automatically work with the W,A,S,D keys and even a controller Input with the analog sticks. This is the simplest and most efficient way into setting up a character controller that will be using the animator.
Part 3 — Combat Controller
If you want to download the Unity3D package you can download it on my Patreon! Thanks for reading and I hoped I helped some people learn something new and save some time :]