Player Controller | Unity3D

Image for post
Image for post

Introduction
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)

Mixamo Link

The Animator
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”.

Image for post
Image for post

Inside of our parameters within our Animator, we will add two floats, “VelX” and “VelY” to get our Horizontal and Vertical positions.

Image for post
Image for post

Player Controller
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:

Image for post
Image for post

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.

Image for post
Image for post

Now we add our “Move()” command into our update underneath our var X & Y so that it looks like this:

Image for post
Image for post

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.

Image for post
Image for post

Part 3 — Combat Controller

Download Source

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 :]

Patreon Link:
https://www.patreon.com/posts/32515666

Written by

Indie Game Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store