Setting up a Character
For the character script to work correctly you’ll need to add an animator and make sure the Apply RootMotion is off. You should also add a Rigidbody, it must be set as kinematic. Gravity is set on the character mover. The Character controller is also required as it is used by the default Character Mover script. Here is an example of the character in the inspector:
Character Mover
The default character mover is used to move the character respective to the camera. It takes in a speed value to change the movement speed of the character.
These are the relevant interfaces for character mover and any other object that moves.
/// <summary>
/// Interface for the character mover.
/// </summary>
public interface ICharacterMover : IParentMover
{
Vector3 CharacterInputMovement { get; }
bool IsJumping { get; }
}
/// <summary>
/// Interface for the parent mover.
/// </summary>
public interface IParentMover : IMover
{
void AddExternalMover(IMover mover);
void RemoveExternalMover(IMover mover);
}
/// <summary>
/// Interface for the mover.
/// </summary>
public interface IMover
{
void Tick();
Vector3 Movement { get; }
void SetParentMover(IParentMover parent);
}
We make good use of the IParentMover and IMover interfaces to allow you to add external movements to the character. Examples are moving platforms and knock-back from attacks.
Character Rotator
The default character rotator rotates the character in the direction of the input. This allows the character to always look where it is trying to go.
No interfaces are used. Only a Tick function that can be overridden.
Character Input
The character input allows you to map actions to inputs. You’ll most likely need to add your own if you wish to extend functionality.
All it requires is to inherit the ICharacterInput script.
The character input expects Horizontal, Vertical inputs as well as Jump, interact and using/equipping/dropping items
public interface ICharacterInput : IItemInput
{
float Horizontal { get; }
float Vertical { get; }
bool Jump { get; }
bool Interact { get; }
}
public interface IItemInput
{
bool UseEquippedItemInput(IUsableItem item, int actionIndex);
bool UseItemHotbarInput(int slotIndex);
bool DropItemHotbarInput(int slotIndex);
}
Character Animator
The character animator will animate the character depending on the state of the other character controls. Example: animate the character moving when the character mover is moving the character.
The Animator is separated in three layers:
Locomotion:
Locomotion is used for movement, jumping and interaction
Actions:
Actions is used for attacks. We use three item parameters.
- Item : The item index. In the demo pickaxe -> 1, snowBall -> 2
- ItemActionIndex : The index of the action to perform for the item specified. pickAxe Swing -> 1, snowball throw -> 1
- ItemAction : Trigger the action once the other two parameters are set.
The item an itemActionIndex values for the pickaxe and snowball are defined in the CharacterAnimator script.
Reaction:
Reaction is used when getting hit or being killed. It overrides all other animations from layers bellow.
More about the character
The character script works with other components like the Damageable, interactor and inventory.