Character

Character

The character script is a MonoBehaviour which is used to group the scripts that will control the character. The control scripts are each used to control a very specific piece of the character. For example we have the character mover which is used to move the character and the Character Rotator which only deals with rotating the character. Keeping these controls modular and well separated allows you to swap them out by something else without having to change code all over the place.

For people who do not want to write a line of code, you are free to use the components available within the demo. But for those who are ready to give it a shot, try adding a new character controls and you’ll find that it is easier than you’d think thanks to the way the character script is organized.

For example you could override the function below of the Character script and replace the characterInput by your own script.

/// <summary>
/// Assign the controllers for your character.
/// </summary>
protected virtual void AssignCharacterControllers()
{
    m_CharacterMover = new CharacterMover(this);
    m_CharacterRotator = new CharacterRotator(this);
    m_CharacterAnimator = new CharacterAnimator(this);
    m_CharacterInput = new CharacterInput(this);
}

Most beginner Unity programmers believe that you must write a MonoBehaviour and put all your logic in one big Update function so that it can be processed on each frame. This can become quickly very messy. To keep things clean you can instead use the “Tick” pattern, which consists of calling a Tick function on a class within the MonoBehaviours Update function like so:

protected​ ​virtual​ ​void​ ​Update​()
{
    m_CharacterMover.Tick();
    m_CharacterRotator.Tick();
    ...
}

This way the character control script can run logic every frame without being a MonoBehaviour.