Setting the Character Controller

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.

Getting ready

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.

How to do it...

  1. Access the Scripts folder in the Project panel. Select PlatformInputController in the Scripts folder of the Character Controller and drag it onto the game character in the scene.
    How to do it...
  2. The Character Controller, the Character Motor, and the Platform Input Controller components are added to our character. Now we can tweak their properties one by one to get the controls we like.
    How to do it...
  3. Let's begin by inspecting the Character Controller component. The Slope Limit parameter defines how steep the ground is so that the character is able to walk on it. Surfaces steeper than this value in degrees are considered walls and block the character. Very useful!
  4. The Step Offset field defines the maximum height of objects that the character can step over. Unity manually suggests to keep this value between 0.1 and 0.4, assuming a standard character with a height of 2.0 in Unity units. And so we do!
  5. The Skin Width field is a crucial parameter to prevent the character from getting stuck on walls and other obstacles should their respective colliders compenetrate each other. Very large values my cause the character to pierce into obstacles, while very small values may lead to no collision detection at all, so tweaking may become necessary with this parameter.
  6. Center, Radius, and Height are pretty self-explanatory. Center defines the origin of the capsule collider that surrounds the mesh. Radius and Height are used to tweak the dimensions of the capsule to adapt it to the character model.

    Take a look at the following screenshot to check the configuration of the Character Controller component:

    How to do it...
  7. The Character Motor and Platform Input Controller scripts are predefined scripts that have been added with the package to the project. Character Motor defines the actual behavior of the character by controlling parameters such as its horizontal speed and acceleration, as well as jump height. Platform Input Controller, on the other hand, takes care of taking the input from the player and turning it into actual character actions on screen. We suggest you check out the forums to know more about these two important scripts. The following links are good ones to start with:

How it works...

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.

There is more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.21.158