๐Ÿงจ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

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.

Reaction Scriptable Var

To create a new Reaction go to the Menu:

Create > Malbers Animations > Reaction Var

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.

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

Scripting

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

This is the correct syntax:

[SerializeReference,SubclassSelector]
public Reaction reaction;

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

To call a reaction simply use this code:

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

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:

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!!
    }
}

Last updated