Unity has lot of inbuilt features that allows us to move our characters in the game. Unity’s built in Character Controller is really useful for beginners wanting to move their character with minimal code. You can literally add a component to the player and move it with a single line of code. In this tutorial, we will see how to use the built in Unity Character Controller and what are its advantages and disadvantages.
Video Tutorial
How to use Unity’s built in Character Controller
Select the character that you want to move and click on Add Component. Search for Character Controller and click Add. The Built-in Character Controller has a collider of its own. So, if you have already attached a collider then you need to remove it.
Setting the Properties of Character Controller
Slope Limit
This specifies the angle of slope up to which the Character Controller will climb. Any slope with an angle greater than the Slope Limit the Character Controller will not climb it.
Step Offset
Step offset is the height of the obstacle that the Character Controller will climb. If the Obstacle is taller than the Step Offset then the Character Controller will not step on it. Step Offset should be less than the height of the game object.
Skin Width
Skin Width a small offset around the player that acts like a second layer of Collider. This is kept to avoid characters from entangling into other objects. Skin width is ignored when the collision is from the sides.
Min Move Distance
Min Move Distance is the minimum distance required between the current position and the target. If the target is near and the distance is less than min move distance then the Unity Character Controller will not move.
Center, Radius and Height
All these properties are related to the collider inside Character Controller. You can set these based on the actual player size.
Moving the player with Unity Character controller
You will need a simple script to make the character move. So, lets click on Add component and add a new script called “Character_move”.
The first step is to declare a variable of type Character Controller and a variable of type Vector3 for speed.
Next, we will get the Character Controller using GetComponent.
For the speed vector, we will get the horizontal and the vertical axis as keyboard input and give it as input to Character Controller.
Character Controller script
using UnityEngine;
public class Character_move : MonoBehaviour
{
CharacterController cha;
Vector3 move_speed;
void Start()
{
cha=GetComponent<CharacterController>();
}
void Update()
{
move_speed=new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
cha.SimpleMove(move_speed);
}
}
We have used the SimpleMove method for our script. You can also use the Move method to move your character.
Difference between SimpleMove and Move in Unity Character Controller
Move | Simple Move |
---|---|
Does not use Gravity | Uses gravity |
Movement is in absolute values | Move is based on Time.deltatime |
Possible to move in y direction | Ignores Y input |
Best Suited if Player needs to jump | Best suited if no jump is required. |
Collision Detection using Unity Character Controller
Since the regular collider and Rigidbody are not present, we cannot use the Unity’s Collision detection system. The Character Controller has its own collision detection system. There are collision flags to detect Collision on a Character Controller.
The collision is only detected if the player is moving towards the colliding object. If the object comes and hits the player then the collision is not detected.
The Following Collision flags are returned true depending on where the collision is detected.
Collision point | Flag |
---|---|
Below | CollisionFlags.Below |
Above | CollisionFlags.Above |
Sides | CollisionFlags.Sides |
You can add the collision flags as condition to an if statement and put in your required codes.
Check if Character Controller is grounded
To check whether the Character Controller is touching the ground you can either use the below collision flag or you can use the isGrounded property.
if (cha.isGrounded)
{
Debug.Log("CharacterController is grounded");
}
Character Controller Collision handling
Collision flags are really handy but what about knowing which objects the Character Controller has collided with. Since the OnCollisionenter and OnTriggerEnter won’t work with Character Controller, Unity has a custom function called “OnControllerColliderHit” which works in the same fashion.
This function is called if the Character Controller is moving towards the gameobject and hits on it.
Collision script
void OnControllerColliderHit(ControllerColliderHit col)
{
Debug.Log(col.collider.gameObject);
}
Disable Collision on Character Controller
You can set detectCollisions to false if you don’t want the Character Controller to detect collisions.
cha.detectCollisions = false;
Implementing jump using Character Controller
As we saw in the previous section, the SimpleMove property of Character Controller does not allow the character to jump. So, we need to use the Move property but Move doesn’t use gravity. That means we need to add a custom gravity to our script.
We can add gravity by adding the Y axis with a gravity value multiplied by Time.deltaTime.
Then we can set the Y speed to the Character Controller’s speed vector when the jump button is pressed. You can change the jump_speed and gravity to control the jump.
Jump script for Character Controller
using UnityEngine;
public class Character_move : MonoBehaviour
{
CharacterController cha;
Vector3 move_speed;
float gravity=-9.8f;
float jump_speed=0.5f;
void Start()
{
cha=GetComponent<CharacterController>();
}
void Update()
{
move_speed=new Vector3(Input.GetAxis("Horizontal"),move_speed.y+gravity*Time.deltaTime,Input.GetAxis("Vertical"));
if(Input.GetKeyDown(KeyCode.Space))
{
move_speed.y=jump_speed;
}
cha.Move(move_speed);
}
}
Advantages of Unity Character Controller
- Simple to Implement.
- Collision detection without Rigidbody in Unity.
- Different Collision flags for bottom, side and top collisions.
- Slope and step navigation included.
- Two different types of movement using Move and SImpleMove.
Disadvantages of Unity Character Controller
- Collider won’t rotate in any condition.
- Collision is not detected if the Character Controller is not moving.
- Implementing Jump requires a little extra work.