Spell Run – Physics: Rigidbody vs CharacterController

Let’s start the game.
I always try to make small playable steps, as small as I can, because it’s always better to find any issues as soon as possible.
Now, the original idea was about a ball, not wizards, rolling down a maze. So, I simply began creating a sphere, adding a RigidBody to it, and created quad below it.

Our very first visual glimpse of the game

Pressing play now simply brings the ball down to the quad, and it stays there. Let’s add some motion.
We want the ball to move forward, at all times, at a constant speed. With a rigidbody that is fairly easy to do: we add some Torque in the X axis:

Now the ball rolls forward until it reaches the end of the quad and falls down. Great start!
I wanted to add some basic interactivity to play something. I want to create tiles with the keyboard. I know the game is going to be mobile but I want to speed up any testing and using the keyboard is the fastest way for me.
To be able to Instantiate floor tiles, I need a Prefab. For now, I create a quad and make a prefab out of it. I’m going to code creation for a straight tile when the W is pressed, a left tile when the A is pressed, and a right turn tile when the D is pressed. For the left and right tiles I’m going to simply rotate the instantiated tile by 45 degrees to the corresponding side. But we also need to keep track of each tile we spawn in order to spawn the next right after it. Otherwise, they will all stack up. Here’s how it looks:

Three tiles created using the keyboard: two straights and one right

First order of business. The ball does not rotate to follow the right tile. Thats expected. Why would it? There’s nothing to actually make it turn.
So let’s go ahead and improve our prefab a bit and add some walls to create an enclosed path for the ball to follow. After some tweaks, here’s what we came out with:

A floor tile with walls

Here’s our three tile sequence looks like with walls

As you can immediately see, we have a problem with the rotating tiles: there’s a hole there. We’ll get to that later. But now, our ball collides with the walls and follows the path… in its own way…
The ball always moves forward. But when we rotate we notice the ball speed is slighty reduced. And if we keep and keep turning, the ball stops completely.
This happens because the ball, when it hits a wall, it slides instead of actually rotating, and since we are telling the ball to always go forward, and its forward vector does not change, it simply collides against the wall and cannot move.
We have a choice to make: do we want the ball to be governed by Unity’s physics and move and rotate it by applying forces? Or do we want full control over it.
We can make it rotate by applying forces, but its going to be a bit complex to code and specially test. It may be much better to have full control over the ball, and here’s where the CharacterController comes to our rescue.
The CharacterController will only move when you tell it to move. It will be constrained by collisions, but it will not be governed by real physics like our ball. This makes things so much easier for us, because we can simply tell the ball to move and rotate as much as we want, we can stop it short or make it fly, and it will still collide with our world.
We choose to have full control. That is way I’ve chosen to use a CharacterController to move the ball and rotate it. We applied that change and it looks great, but using a CharacterController brings an issue we didn’t have before: the ball does not spin when moving forward now.
That’s because to Unity, it doesn’t matter what the ChracterController is, it just moves when you say it moves. If its a ball or a cube.
So we’ll need to make the ball rotate in two axis: X axis so the ball rotates as it moves forward (like a car wheel), and on the Y axis when it needs to rotate to the left or to the right. We deal with that in our next article.
Don’t forget to follow us on twitter for news regarding articles and game development [twitter-follow screen_name=’indelvestudios’]