Interactors and Interactables

Interactors and Interactables

Interactors and Interactables interfaces are used for components to interact with each other. Interactors can select, unselect and interact with interactables. When the interactable is selected, unselected or interacted with it sends an event, which you can use to do any number of interesting things. You could interact with an object to pick up an item or action a lever that opens a path, etc…

/// <summary>
/// The interactable interface used to be interact with by an interactor.. 
/// </summary>
public interface IInteractable
{
    bool IsInteractable { get; }
    bool Interact(IInteractor interactor);
    bool Select(IInteractor interactor);
    bool Unselect(IInteractor interactor);
}

/// <summary>
/// The interactor allows you to interact with interactables.
/// </summary>
public interface IInteractor
{
    void AddInteractable(IInteractable interactable);

    void RemoveInteractable(IInteractable interactable);
}

/// <summary>
/// The character interactable has a reference to a character.
/// </summary>
public interface ICharacterInteractor : IInteractor
{
    Character Character { get; }
}

For example we can interact with the crystals in the demo scene to move the moving platforms.

Image

Usually an interactable will be paired with an InteractableBehaviour which defines what happens when the interactable is interacted with. You can write your own InteractableBehaviours to create custom features. You can also use the Unity Actions directly in the inspector to trigger some functions on interaction without writing a line of code.

Image

As you can see in the picture above we are using an moving platform interactable behavior. An interactable behavior is a component that listens for events on an interactable component. It can be easily extended.