> For the complete documentation index, see [llms.txt](https://malbersanimations.gitbook.io/animal-controller/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://malbersanimations.gitbook.io/animal-controller/main-components/reactions.md).

# Reactions

## Overview

Reactions are snippets of code you can add to your scripts to execute more precise logic on the animal or on any components. Instead of using Unity Messages or Unity Events to connect them.

## How to use them

### Reactions Component

You can add the Reactions Component to any GameObject to access them

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2Fu2LNpIhbMiqr7X8p7U4E%2Fchrome_AzjPaw4UxR.gif?alt=media&#x26;token=bb3eb302-cf2e-4927-9570-be805727abe2" alt=""><figcaption></figcaption></figure>

Once you have added you can use the `React(Component Target)` method to activate it. Whether is via a Unity Event or by code. You need to give the Reaction a Target to apply the reaction.

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2FU4ezdmsJPeAC7ZszChYO%2FUnity_wD6enzPmku.gif?alt=media&#x26;token=bc0377ec-d116-4bcc-8498-c251bfb9c31a" alt=""><figcaption></figcaption></figure>

### Reaction Scriptable Var

To create a new Reaction go to the Menu:

Create > Malbers Animations > Reaction Var

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2FXmiD21EDjUbAbmJXHIPQ%2Fimage.png?alt=media&#x26;token=c606ade5-2d48-4694-b70c-41111b841510" alt=""><figcaption></figcaption></figure>

To make use of the Reaction Var you can add them to any Unity Event that has a GameObject, Monobehaviour, Transform, or Animal as a parameter.

![Using a Speed Reaction to Activate the Walk Speed Modifier to the Animal](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2FpmPLvaahImP3NXHoloN2%2FUnity_I6uTPxiBXi.gif?alt=media\&token=1cffe6fa-d47f-42a9-8ed6-e5b96197021f)

E.g. You can Disable the Jump State when entering a room ("Box Collider"). and enable it back when exiting:

{% embed url="<https://streamable.com/xnib62>" %}

### Scripting

You can also add a reaction to your code and use it just like any other event.

This is the correct syntax:

<pre class="language-csharp" data-full-width="false"><code class="lang-csharp"><strong>[SerializeReference,SubclassSelector]
</strong>public Reaction reaction;
</code></pre>

You need to add `[SerializeReference,SubclassSelector]` to display it right on the inspector.

To call a reaction simply use this code:

```csharp
reaction?.React(component); //Add a Target to the reaction, [Component]
//OR
reaction?.React(gameObject); //Add a Target to the reaction, [GameObject]
```

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2FJD7d5NYzbbX3XXrCu4J6%2Fimage.png?alt=media&#x26;token=0367c4b4-e753-42a0-a454-2328332521b4" alt=""><figcaption><p>Example of MDamageable Component using reactions.</p></figcaption></figure>

## Creating a new Reaction Type

You can create your own reactions. Here's a quick example of how to create a reaction that enables or disables a collider:

{% embed url="<https://streamable.com/2wpcai>" %}

{% code overflow="wrap" %}

```csharp
using MalbersAnimations;
using MalbersAnimations.Reactions;
using System; 
using UnityEngine;

[System.Serializable] //Needs to be Serializable!!!!
[AddTypeMenu("My Reaction/Enable Collider")]
public class EnableColliderReaction : Reaction
{
    //set the Type of component this Reaction Needs
    public override Type ReactionType => typeof(Collider); 
    
    public bool enable;

    protected override bool _TryReact(Component reactor)
    {
        Collider collider = reactor as Collider; //Cast the reactor as collider type.
        collider.enabled = enable;              //set the enable paramater

        return true; //Reaction succesful!!
    }
}

```

{% endcode %}

***

## Reaction Reference

Every built-in reaction, one page each, grouped by its menu path.

### [Animal Reactions](/animal-controller/main-components/reactions/animal-reactions.md) — 14

Everything that drives an `MAnimal`: [Mode](/animal-controller/main-components/reactions/animal-reactions/mode.md), [State](/animal-controller/main-components/reactions/animal-reactions/state.md), [Stance](/animal-controller/main-components/reactions/animal-reactions/stance.md), [Speeds](/animal-controller/main-components/reactions/animal-reactions/speeds.md), [Movement](/animal-controller/main-components/reactions/animal-reactions/movement.md), [Teleport](/animal-controller/main-components/reactions/animal-reactions/teleport.md), [Gravity](/animal-controller/main-components/reactions/animal-reactions/gravity.md), [Add Force](/animal-controller/main-components/reactions/animal-reactions/add-force-to-animal.md), the three bulk Enable-Disable reactions, and more.

### [Unity Reactions](/animal-controller/main-components/reactions/unity-reactions.md) — 12

Standard Unity components: [GameObject](/animal-controller/main-components/reactions/unity-reactions/gameobject.md), [Behaviour](/animal-controller/main-components/reactions/unity-reactions/behaviour.md), [Collider](/animal-controller/main-components/reactions/unity-reactions/collider.md), the three Rigidbody reactions, [Animator SetParameter](/animal-controller/main-components/reactions/unity-reactions/animator-setparameter.md), [Particle System](/animal-controller/main-components/reactions/unity-reactions/particle-system.md), [Sound](/animal-controller/main-components/reactions/unity-reactions/sound-audio.md), [Messages](/animal-controller/main-components/reactions/unity-reactions/messages.md), and scene persistence.

### [Malbers Reactions](/animal-controller/main-components/reactions/malbers-reactions.md) — 5

Other Malbers systems: [IK](/animal-controller/main-components/reactions/malbers-reactions/ik.md), [Tags](/animal-controller/main-components/reactions/malbers-reactions/tags.md), [Damageable Set Profile](/animal-controller/main-components/reactions/malbers-reactions/damageable-set-profile.md), [Play Combo](/animal-controller/main-components/reactions/malbers-reactions/play-combo.md), [Animator Sound](/animal-controller/main-components/reactions/malbers-reactions/animator-sound-audio.md).

### [General Reactions](/animal-controller/main-components/reactions/general-reactions.md) — 6

The escape hatches: [\[Member Set\]](/animal-controller/main-components/reactions/general-reactions/member-set.md), [\[Member Get\]](/animal-controller/main-components/reactions/general-reactions/member-get.md), [\[Event\]](/animal-controller/main-components/reactions/general-reactions/event.md), [\[Reaction Var\]](/animal-controller/main-components/reactions/general-reactions/reaction-var.md), [Reposition](/animal-controller/main-components/reactions/general-reactions/reposition.md), [\[Debug\]](/animal-controller/main-components/reactions/general-reactions/debug.md).

### [Tool Reactions](/animal-controller/main-components/reactions/tool-reactions.md) — 4

Aiming and alignment: [Aim](/animal-controller/main-components/reactions/tool-reactions/aim.md), [Align Look At](/animal-controller/main-components/reactions/tool-reactions/align-look-at.md), [Align to Aim Direction](/animal-controller/main-components/reactions/tool-reactions/align-to-aim-direction.md), [Projectile Thrower](/animal-controller/main-components/reactions/tool-reactions/projectile-thrower.md).

### [Scriptable Reactions](/animal-controller/main-components/reactions/scriptable-reactions.md) — 3

Var Listeners: [Bool](/animal-controller/main-components/reactions/scriptable-reactions/set-bool-var-listener.md), [Float](/animal-controller/main-components/reactions/scriptable-reactions/set-float-var-listener.md), [Int](/animal-controller/main-components/reactions/scriptable-reactions/set-int-var-listener.md).

### [Stat Reactions](/animal-controller/main-components/reactions/stat-reactions.md) — 2

[Stat Modify](/animal-controller/main-components/reactions/stat-reactions/stat-modify.md) and [Stat (Enable-Disable)](/animal-controller/main-components/reactions/stat-reactions/stat-enable-disable.md).

***

## Documented elsewhere

* **Super Item Reaction** and **Pouch Manager Reaction** — on the [Super Item System Reactions](https://malbersanimations.gitbook.io/animal-controller/super-item/super-item-system/reactions) page.
* **Weapon Manager reactions** (Equip-Unequip, Holster, Drop, Global Actions) — with the [Weapon Manager](https://malbersanimations.gitbook.io/animal-controller/main-components/weapon-manager).

***

## Three rules that apply to all of them

**Nothing restores itself.** A reaction that locks, disables or overrides needs a matching one that undoes it, usually on the `On Exit` of whatever set it.

**String matching fails silently.** Several reactions identify their target by name rather than by an ID asset — speed sets, IK sets, damage profiles, sound events. A typo does nothing at all, with no error. Check spelling first when a reaction appears inert.

**Prefer the specific reaction.** Where a dedicated reaction exists, use it over a generic one. [Teleport](/animal-controller/main-components/reactions/animal-reactions/teleport.md) rather than [Reposition](/animal-controller/main-components/reactions/general-reactions/reposition.md) for an Animal; the [Animal reactions](/animal-controller/main-components/reactions/animal-reactions.md) rather than [Animator SetParameter](/animal-controller/main-components/reactions/unity-reactions/animator-setparameter.md); the damage pipeline rather than [Stat Modify](/animal-controller/main-components/reactions/stat-reactions/stat-modify.md) for weapon damage.
