> 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/global-components/interactor.md).

# Interactor

Last updated AC v1.5.3

## Overview

This component stores the Index and GameObject of an [Interactable](/animal-controller/global-components/interactable.md) and invokes events that can be used to create Reactions according to each Interactable value. It also executes the Interaction logic on the Interactable.

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2Fwfly6aWaIglbo0r1n0E4%2Fimage.png?alt=media&#x26;token=7a9e70b1-d595-49a2-b387-8aab4196d783" alt=""><figcaption></figcaption></figure>

The inspector is split into three tabs: **General**, **Events** and **Reactions**.

## How to use it

Place the Interactor on your character and add an **Interaction Area** (Collider). This will allow it to detect Interactables in the scene. When an Interactable is inside the Collider, the Interaction logic can be activated.

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2FNRjXCBKp8uNmKm7GwYuy%2FUnity_4225hSiYKa.gif?alt=media&#x26;token=a7cf3d05-918f-4e06-b127-e2542aa93945" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
Either the **Interactor** or the **Interactable** gameobject needs to have a **Rigidbody** attached. Otherwise, the Interaction will not occur.
{% endhint %}

To activate the interaction you can add an Input to your Malbers Input Component and connect it directly to the Interactor's `Interact()` method.

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2Fk0vJxGXO8q9BwFee3JHB%2Fimage.png?alt=media&#x26;token=a9e2e5c1-1742-44f0-b2c1-9b3d9ff8be54" alt=""><figcaption></figcaption></figure>

If the Interactable is set to be **Automatic** then the interaction will not need manual activation: it fires the moment the Interactable is focused.

Use the Reactions lists to have different reactions for different Interactable IDs.

## How it Works

The Interactor does not listen to `OnTriggerEnter` itself. On enable it attaches a [Trigger Proxy](/animal-controller/global-components/trigger-proxy.md) to the **Interaction Area** collider and subscribes to it, passing down its own **Layer**, **Trigger Interaction** and **Tags**.

It keeps two sets at runtime:

* **Inside** — every Interactable currently inside the Interaction Area, focused or not.
* **Focused** — the Interactables the Interactor is currently focusing. With **Single Focus** on, this is at most one.

### Single Focus

**Single Focus** is **on** by default and changes how focus is handed around:

* The **newest** Interactable to enter the area takes the focus. The previous one receives **On Unfocused** but stays inside as a candidate.
* When the focused Interactable leaves, is picked up, or is disabled, the **closest** Interactable still inside the area is focused automatically.

With Single Focus **off**, every Interactable inside the area stays focused at the same time, and `Interact()` fires on all of them.

{% hint style="info" %}
The "closest" candidate is measured from the **centre of the Interaction Area bounds**, not from the character's pivot.
{% endhint %}

{% hint style="warning" %}
An Interactable made of several colliders is only unfocused when its **last** collider leaves the area. The Interactor asks the Trigger Proxy whether another collider of the same Interactable is still inside before releasing it.
{% endhint %}

## Parameters

### General

#### Layer

What layer will be checked on the Interactables.

#### Trigger Interaction

Does the Interactor interact with triggers or just colliders?

#### Tags

Malbers [Tags](/animal-controller/scriptable-architecture/scriptables/tags.md) to check on the Interactables. Leave it empty to allow everything. These Tags are merged into the Trigger Proxy created on the Interaction Area.

#### ID

Index of the Interactor. This parameter is received by the Interactable when `Interact()` is called, so the Interactable can check who activated its logic.

#### Interaction Area

Reference for the Collider used to find Interactables \[On Trigger Enter]. It is forced to **Is Trigger** automatically.

#### Single Focus

Focus only one Interactable at a time. On by default. See [Single Focus](#single-focus) above.

#### Debug

The bug icon next to the tabs. Logs every focus and interaction. In Play Mode the inspector also lists the **Focused Item** and everything **Inside** the area, with their IDs.

## Events

On the **Events** tab.

<figure><img src="https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2F6dEu7Gd04T8hfbW1P84o%2Fimage.png?alt=media&#x26;token=a82f8b87-795b-461b-841c-f4fda6852131" alt=""><figcaption></figcaption></figure>

### On Interact with (GameObject)

Invoked when the `Interact()` method succeeds. Sends the Interactable GameObject as parameter.

### On Interact with (Int)

Invoked when the `Interact()` method succeeds. Sends the Interactable Index as parameter.

### On Focused (GameObject)

Invoked when an Interactable takes the focus. Sends the Interactable GameObject.

### On Unfocused (GameObject)

Invoked when an Interactable loses the focus: it left the area, it was picked up, or another Interactable took the focus from it. Sends the Interactable GameObject.

{% hint style="info" %}
`Restart()` invokes **On Focused** and **On Unfocused** with a **null** GameObject to let UI elements clear themselves. Listeners must handle null.
{% endhint %}

## Reactions

On the **Reactions** tab there are now **two** lists.

`[Insert Image - The Reactions tab with the Reactions and Focus Reactions lists]`

### Reactions

Fired when the Interactor **interacts** with an Interactable.

#### Description

Free text label for the row.

#### Is

How the Index is compared: Equal, Not Equal, Greater, Less, Greater Equal, Less Equal.

#### Index

Interactable Index to compare against. Set it to **0** or a negative value to run this reaction for **all** Interactables.

#### Reaction

[Reaction](/animal-controller/main-components/reactions.md) applied on the Interactable GameObject when the Index matches.

### Focus Reactions

Same shape, but fired on **focus** instead of interaction. Each row has **two** Reaction slots:

* **On Focused** — applied on the Interactable when it takes the focus.
* **On UnFocused** — applied on the Interactable when it loses the focus.

Use this pair to highlight an Interactable, show a key prompt, or outline a mesh while the character is looking at it.

{% hint style="info" %}
Earlier versions had a single Reactions list with a **Target** field. The Target is gone: the Reaction is now applied directly on the Interactable that matched, and [Reaction2](/animal-controller/main-components/reactions.md) carries its own targeting options.
{% endhint %}

## Public Methods

Callable from UnityEvents or code:

* `Interact()` — interact with everything currently focused, in reverse order.
* `Interact(GameObject)` / `Interact(Component)` / `Interact(IInteractable)` — interact with one specific Interactable.
* `Focus(IInteractable)` / `UnFocus(IInteractable)` — focus or release one Interactable by hand.
* `FocusNext()` — Single Focus only. When nothing is focused, focuses the closest active Interactable still inside the area.
* `RemoveFocusedItem(IInteractable)` — drop an Interactable from both the focus and the candidates, e.g. because it was just picked up, then hand the focus to the next one inside.
* `Restart()` — clear the focus and the candidates and invoke the focus events with null.

Properties:

* `ID` — the Interactor Index.
* `Active` — enable/disable the Interactor.
* `ItemInFocus` — true while at least one Interactable is focused.
* `FocusedInteractables` — the focused set.
* `Inside` — every Interactable currently inside the area.
* `Proxy` — the [Trigger Proxy](/animal-controller/global-components/trigger-proxy.md) created on the Interaction Area.

Code-level hooks (`System.Action<IInteractable>`): `OnFocusing`, `OnUnFocusing`, `OnInteract`.
