# Character brain

`CharacterBrain` is a component responsible for handling all character actions, regardless of whether they come from the player or not.

![Representation of the Human, the AI and the brain.](https://3428853154-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvxVSjyzvP6F7c9h_Hu%2F-LyvxXmZLQz0JRCLXEnJ%2F-LyvyGGzsu0oWoJmo2L2%2FcharacterBrain.png?alt=media\&token=a91c280f-944d-4673-8fca-682bc2c8c4fd)

If you are familiar with the Unity's "new" input system, you probably know what an action is. An **action** is just a link between an input signal (e.g. press the jump button) and an output result at the gameplay level (e.g. jump).

All the available actions are predefined in a structure and then updated by the *CharacterBrain* component at runtime. This approach create a level of abstraction between the inputs (GetKey, GetButton, etc.) and the character actions themselves (jump, move forward, etc.).

## Actions

### Types

CCP's Implementation supports three types of actions

| Action type   | Description                                              |
| ------------- | -------------------------------------------------------- |
| BoolAction    | A toggle, this action can be pressed or not pressed.     |
| FloatAction   | A 1D Value from -1 to 1 (similar to `GetAxis`from Unity) |
| Vector2Action | A 2D Value from, basically a combination of two axis.    |

{% hint style="info" %}
Note that the BoolAction value is true or false (a bool). If you need to know if that action was "started" or "canceled" (e.g. was the jump button pressed?), you need to get the **Started** or **Canceled** property respectively.
{% endhint %}

### Character actions struct

These actions are predefined and grouped together inside a struct. Each public field represents a particular action.

```csharp
public struct CharacterActions 
{
	// Bool actions
	public BoolAction @jump;
	public BoolAction @run;
	public BoolAction @interact;
	public BoolAction @jetPack;
	public BoolAction @dash;
	public BoolAction @crouch;
    
  //...
}
```

{% hint style="info" %}
You can visualize the actions values in runtime in the inspector (Human or AI).
{% endhint %}

![](https://3428853154-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvxVSjyzvP6F7c9h_Hu%2F-M9XyFr6IuU2qH8yusG3%2F-M9XymHujl37cCNo7c1a%2Fimagen.png?alt=media\&token=16f32fd0-48e3-4b02-9aa6-49e85813e6ef)

{% hint style="info" %}
CCP includes a *ScriptableObject* asset whose only job is to modify this *CharacterAction* struct. The actions included by default were generated using the **Default Character Actions** asset.
{% endhint %}

{% hint style="danger" %}
Have in mind that adding/removing actions might affect the scripts from the Demo content since these scripts are using the default CCP actions.
{% endhint %}

## Brain types

![Brain modes in the inspector.](https://3428853154-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvxVSjyzvP6F7c9h_Hu%2F-LyvyOLIKJOklGJLWNsA%2F-LyvyU4_UOLc_8FnvyqT%2FBrainModes.png?alt=media\&token=daba0ce2-f503-45c6-b752-a7b961ae833b)

### Human brain

If the brain is set to *Human*, actions will get updated based on input devices (keyboard, mouse, joystick, UI, etc). The component responsible for this is called `InputHandler`, and it must be implemented specifically for each input system needed.

By default, the brain allows the user to use two input handlers that come with the asset, or a custom one.

![Available human input types.](https://3428853154-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvxVSjyzvP6F7c9h_Hu%2F-Lyvyk-QSFwCh-EefBag%2F-LyvzZqtEL2gctckzZ5f%2FhumanInputType.png?alt=media\&token=5251962e-c591-442c-bde2-58e212e81334)

| Human input type    | Description                                                                                                                                                           |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Unity Input Manager | It reads input data directly from Unity's Input manager (old system).                                                                                                 |
| UI\_Mobile          | It looks for all UI-based input components in the scene (`InputAxes` and `InputButton`). These components are responsible for converting UI events into input values. |
| Custom              | An `InputHandler` custom implementation.                                                                                                                              |

### AI brain

In this mode, the brain defines actions through code by using a `AIBehaviour`.&#x20;

![Example: AI behaviour using a sequence behaviour (Demo content).](https://3428853154-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LvxVSjyzvP6F7c9h_Hu%2F-M9XseN2KHG4sQTj8tHQ%2F-M9XxMD9oNUuUDT2O0bW%2Fimagen.png?alt=media\&token=5ff9b6cb-8ebb-4b7e-898b-9d1687027218)
