The Character Controller is designed for the first- and third-person control systems. It comes with movement control in all four directions, jump controls, and independent control of the camera, so the player can run in one direction and shoot in another.
A pretty clear way to describe the Character Controller is by saying that it is very precise with regard to movement, though it lacks fluidity: the character accelerates to maximum speed in the blink of an eye, turns on a dime, and stops the very moment you release the button. In other words, the feeling you get using a Character Controller is not very realistic, and to make it more fluid so it looks realistic, you need to turn to coding.
The Character Controller also doesn't implement physics by itself. It detects collisions but doesn't push or apply forces to the other GameObjects in the scene unless you program it to do so. As a matter of fact, it wouldn't even be affected by gravity if the scripts attached to it didn't make it!
As for collisions, the Character Controller only works with static Capsule Colliders. This means that it is specifically designed for things that resemble humanoid characters and that the Capsule Collider attached to the controller cannot be rotated when the mesh does.
On the other hand, the Character Controller has a pretty useful parameter called isGrounded
that states whether a character has its feet on the ground. The variable is linked to another parameter called Slope Limit to set the maximum verticality of the surfaces we want the character to be able to climb on. Basically, it is a system that automatically prevents the character from walking on walls. Such a useful feature is not available with a Rigidbody.
Step Offset is another very useful parameter provided with Character Controllers that takes care of stating whether the character can step over an object or not. Thanks to this, we can allow the character to step over things such as steps or crates. With a Rigidbody, such elementary behavior must be specifically coded!
Many other properties are available to set up the Character Controller. With the next recipe, we show the most important ones, as we attach and set up a platforming-style Character Controller to our game character. Our plan is to prototype a side-scroller, and Unity offers exactly what we need. If you inspect the folder where we imported the Character Controller package, you should see a JS
script named PlatformInputController in the Scripts folder. That's the script we are going to attach to the game character.
By dragging PlatformInputController onto the game character in the scene, four new elements appear in the Inspector panel: a component, the Character Controller itself, and two scripts—CharacterMotor and PlatformInputController. They are responsible for providing the character with the standard capabilities required by side-scrolling platform games, and by setting their properties, we define the behavior of our game character.
Scripts
folder of the Character Controller and drag it onto the game character in the scene.0.1
and 0.4
, assuming a standard character with a height of 2.0
in Unity units. And so we do!Take a look at the following screenshot to check the configuration of the Character Controller component:
Adding Character Controller to a character basically means equipping the character with means to interact with the game world by defining its collider and how it walks on surfaces. Then, two scripts take care of the rest: Character Motor defines the behavior of the character, how fast it walks, accelerates, runs, or jumps, while Platform Input Controller takes care of turning the input from the player into character controls, specifically designed for platform games.
By inspecting the properties of the Character Motor script, you may have noticed that a parameter called Gravity is available. You tweak this value to increase or decrease how strong the ground attracts the character when it falls.
We stated previously that the Character Controller doesn't implement gravity by itself, and that is still the case—while the Character Controller simulates physics through custom coding instructions of a script attached to the GameObject itself, gravity with the Rigidbody is based on the Unity physics engine, the renewed NVIDIA PhysX.
3.144.21.158