Tip of the Day: Character Controller in Unity
Once starting a new project, the first you would probably do is work on the movement of your Avatar and the question arises. Should I use a character controller or a Rigidbody?
Difference between the two:

The main difference between the two is the freedom of control. The character controller give your player some speed and a collider but no physics while the rigidbody gives controls your players’ physics.

In other words, by using the character controller you are responsible for every physics interaction that happens to your avatar. You have to code everything. Moving the avatar using the Rigidbody allows unity to take control of the physics interaction.
How to setup a Character Controller?
The character controller has a very handy method called “Move” and it takes a direction vector that can be multiplied by speed.
First of all you have to add the Character Controller component to your player, and then create a C# script for movement control.
- Let’s create some variables:
A — Speed to control player speed
B — JumpHeight to control how high the player can jump
C — Gravity (-1) to add gravity physics
D — Two private Vector3s, direction and velocity
E — Finally a float yVelocity to control the y axis of the velocity
2. Get hold of the Character Controller so we can control it
CharacterController _controller;private void Awake()
{
_controller = GetComponent<CharacterController>();
}
3. In the update, set float that will return the player input on the x-asis
var xHorizontal = Input.GetAxis("Horizontal");
4. The controller gives you a hand method to see if the player is grounded, we will use this to allow the player to move and jump while on the floor.
Pseudocode: Assign the x-axis of the direction Vector to the horizontal input and then assign the velocity based on speed.
if (_controller.isGrounded)
{
_moveDirection.x = xHorizontal;
_velocity = _moveDirection * _moveSpeed;
5. In order to jump, we check for player input while grounded, reset the yVelocity to 0 and then set it to the jumpHeight. This will give you a smoother jump and negate the gravity the moment of jumping.
if (_controller.isGrounded)
{
_moveDirection.x = xHorizontal;
_velocity = _moveDirection * _moveSpeed;if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = 0;
_yVelocity = _jumpHeight;
}
}
6. Before calling the Move method from the controller, make sure you are applying gravity to the yVelocity if not grounded, and finally after all of this assign the y-axis of the velocity vector to the yVelocity.
The final movement code should be something like this.

From here you can expand upon this to add a double jump or even a wall jump by using the “OnControllerColliderHit” method.
