Moving a 2D character with Character Controller component


estimated time : 11 mn
ingredients needed : Unity
(this recipe is part of the footvolley game menu )

Detailed Steps

 

00:00 Character Controller component

There are many ways to control a character, but as in our project example we don’t use any particular physics laws for the player, we will use the character controller approach. It’s mainly created for player control that does not make use of Rigidbody physics. We’ll see other ways to control player in other projects.

00:21 Adjusting the capsule collider

This component comes with a capsule collider, let’s adjust its size to fit the player environment (Radius 0.15 / Height 0.1 / Y Center a little less above the ground 0.3 for this sprite).

01:01 Player controller script

Create a PlayerController script, and add a variable for the playerSpeed. Let’s make it public so we can easily tweak the value in the inspector while playing to adjust the speed on the fly. Also making it public will allow other scripts to see it later.

public int playerSpeed;

Now we retrieve the CharacterController component for later use in the code :

private CharacterController _controller;
// Use this for initialization
void Start () {
_controller = GetComponent<CharacterController>();
}

03:14 Moving Player

Let’s see how moving our character with this controller, it has two main public functions: Move and SimpleMove.
The SimpleMove function is used to move the player with speed (we will use this) and ignore the velocity along the y-axis (good for us : our game doesn’t need the player to make any kind of jumps).
So in our update function, we store our inputs on a Vector, and we multiply our vector by the speed value in order to get a basic movement. It’ll be enough for the moment.
void Update () {
Vector3 move = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
_controller.SimpleMove(move * playerSpeed);
}

05:07 Tweaking Inputs

If you try the controls, you’ll see that there is some sliding effect when you stop to move : your player will continue its move a little longer while decreasing its speed. It’s an okay behaviour for most games, but here we don’t want it.
So let’s remove this decreasing speed effect by modifying  the gravity and sensitivity values.
Edit/Project Settings/ Input
for the Horizontal & Vertical groups, set the sensitivity to 10 if you want more reactivity, the gravity to 40, and check the “snap” check box : it ensures you that when you move your controller it’ll send values like 1/-1 directly (no floating values like 0.318).
 
A quick note about Inputs
As always, checking the official docs is always a good move to fully understand what you’re altering.
https://docs.unity3d.com/Manual/class-InputManager.html

06:13 The wall

Now let’s play a little with controls, we’re happy but the player will not be if we let him fall off the field if he goes a little further. Let’s add constraints to the player moves simply by creating invisible walls around the game field.
Let’s create an empty GameObject which will contain the obstacle walls, now create some cubes, dispose them around the gameField, let’s try a little : mmmm, there seems that the player tries to climb a little on our wall, that’s because of slope limit & step offset values of the characterController component. They are used to allow the player to climb litlle steps and slopes. Here we turn them to 0, we don’t want this kind of behaviour for our example.
Once we made our nice 4 walls, just disable the mesh renderer of the cubes in order to turn them invisible but the player capsuleCollider cannot pass through these simple walls.
Wait our little player moves but isn’t animated ? How cheap is that?!
That’s normal folks, when comes to prototyping, we have some interest in the gameplay itself first.
If it works, then we’ll shine later with our fancy animations.

 

A quick note about character controller
The character controller allow you to move your gameObject and allow it to collide with objects. Although the component doesn’t respond to any kind of physics (no rebounds), it comes with gravity.

Leave a Reply