Objective: Create your own physics based controller.
What is a character controller? A Character Controller is a component that allows you to easily implement movement constrained by collisions without needing to deal with a rigidbody.
By creating your own physics based character controller you can control how the character handles physics or how physics effects your environment. Also, doing this means you have to script everything direction, velocity, and gravity.
Select your player and add the character controller component.
The Character Controller contains a collider so delete the base collider on your player object.
If you don’t have one, create and/or open the player script. Inside you need a handle for the controller.
To apply quick and simple horizontal movement we need to define direction based on input, and define the move input based on the direction. First we need the horizontal input. As we did back in the 2D tutorials we assign horizontal movement with the get axis method.
Using this automatically sets the horizontal movements to the “A” and “D” keys. If you want to change these keys you can, you need to navigate to Edit > Project Settings > Input Manager > Horizontal.
With the horizontal input in use now you can define the direction. What is the direction? The direction is a vector 3 local variable set by the horizontal input.
You now have the bare minimum to call your move function.
With this you can now move your player. The horizontal input is set to 1 when active and 0 when not active, meaning your player will move at a speed of one.
We can now add velocity to the player movement. To do this we need to define velocity. Velocity is direction times speed.
With velocity defined you can now set the move function using velocity to give you characters some speed.
Now you have a good understanding on physics based character controller and horizontal movement. In the next tutorial we are going to focus on gravity and jumping. Don’t be afraid to experiment with your codes and I’ll see you in the next tutorial!