Interaction System

The inventory system requires a lot of interactions to pickup items, open chests, open a menu, etc. An interaction system has been created in order to handle these type of interactions.

The interaction system uses the interfaces, IInteractor and IInteractable. An interactable can be selected, deselected, and interacted with. The interactor can add and remove interactables.

For convenience the Inventory Interactor component can be used as the IInteractor and the Interactable component can be used as the IInteractable.

Inventory Interactor

The Inventory Interactor should be a neighbor of the Inventory.
If “Auto Interact” is false the Inventory Interactor will expect an input to trigger the interaction. For convinience use the (Standard) Inventory Input component to set the input.

Important: The Interactor requires a Collider to detect the Interactable.

Interactable

Add the Interactable component on any component the Interactor should interact with. Make sure the Interactor Layer Mask includes the Interactor game object Layer.
Examples of use cases for interactables are : Item Pickup (or any Pickup component), Menu Openers Interactable Behviour (Used to open menus with the Inventory Interactor)

Set “Auto Interact” to true to interact on trigger instead of waiting for the Interactor to interact with the Interactable.

Important: The Interactable component requires a Trigger Collider to detect the Interactor.

Interactable Behavior

Interactable Behaviors are components which sit next to the Interactable component and listen to the events on Select, Deselect and interact.
Examples of Interactable Behaviors are: PickupBase, MenuInteractableBehavior, etc…

Create your own Interactable Behavior

 

/// <summary>
/// Example Interactable Behavior.
/// </summary>
public class ExampleInteractableBehavior : InteractableBehavior
{
    /// <summary>
    /// can the interactor interact with this component.
    /// </summary>
    /// <param name="interactor">The interactor.</param>
    /// <returns>Returns true if it can interact.</returns>
    public override bool CanInteract(IInteractor interactor)
    {
        return base.CanInteract(interactor);
    }
    /// <summary>
    /// The event called when the interactable is selected by an interactor.
    /// </summary>
    /// <param name="interactor">The interactor.</param>
    public override void OnSelect(IInteractor interactor)
    {
        base.OnSelect(interactor);
        // Do something.
    }
    /// <summary>
    /// The event when the interactable is no longer selected.
    /// </summary>
    /// <param name="interactor">The interactor.</param>
    public override void OnDeselect(IInteractor interactor)
    {
        base.OnDeselect(interactor);
        // Do something.
    }
    
    /// <summary>
    /// On Interaction.
    /// </summary>
    /// <param name="interactor">The interactor.</param>
    protected override void OnInteractInternal(IInteractor interactor)
    {
        // If you require the interactor to be an inventory interactor.
        if (!(interactor is IInteractorWithInventory interactorWithInventory)) { return; }
        var inventory = interactorWithInventory.Inventory;
        // Do something.
    }
}