Skip to main content

Class InteractionModel

Holds all runtime state and enforces all interaction rules for the interaction system, independently of how input is delivered or how the result is displayed.

public class InteractionModel : MonoBehaviour, IDataLoggable

Inheritance​

Implements​

IDataLoggable

Remarks​

This class is the single source of truth for interaction state. It tracks all Interactable objects that are physically nearby (via Interactable) and Interactable)), determines which one the player is currently facing (GetActiveInteractable), and manages the full lifecycle of an ongoing interaction — start, per-frame update, completion, and cancellation.

How the active interactable is chosen

Nearby interactables are stored in a HashSet rather than a list because the same Interactable can fire multiple enter/exit events from overlapping colliders — the set ensures each object is registered exactly once. The active interactable is then resolved lazily on each poll interval: the set is filtered to objects whose visibility bounds are inside the camera frustum, and the closest qualifying result is chosen. This lazy approach is necessary because the camera angle changes continuously; caching the result on enter would show the wrong object whenever the player turns away.

This class implements IDataLoggable, timestamping every meaningful state change — interaction start, per-frame update, completion, cancellation, interactable proximity enter/exit, and active-interactable switches. The accumulated log is retrieved by the DataCollector via Collect.

Related components

  • Interactor: The only component that reads from and writes to this class.

Properties​

ActiveInteractable​

The interactable currently presenting its options to the player.

public Interactable ActiveInteractable { get; }

Property Value​

Interactable

Remarks​

The private setter force-finishes any in-progress interaction before switching to the new interactable. This prevents an interaction with object A from completing against object B's option list when the player walks away mid-press.

The setter also moves the OptionsChanged subscription onto the new interactable so option mutations are only observed for the object currently on screen.

Command​

Gets the currently selected interaction command representing an active interaction. This command holds the state and details of the interaction in progress, or is null if no interaction is active.

public InteractionCommand Command { get; }

Property Value​

InteractionCommand

InteractableHashset​

Public accessor for the set of interactables in range. Initialized lazily.

public HashSet<Interactable> InteractableHashset { get; }

Property Value​

HashSet<Interactable>

InteractionInProgress​

Indicates whether an interaction is currently in progress.

public bool InteractionInProgress { get; }

Property Value​

bool

Methods​

Collect()​

Returns a DataLog containing all timestamped interaction events since the last collection, then clears the cache; returns null if no events occurred.

public DataLog Collect()

Returns​

DataLog

FinishInteraction(int)​

Finishes an ongoing interaction using the specified interaction option index.

public void FinishInteraction(int interactionIndex)

Parameters​

NameTypeDescription
interactionIndexintThe index of the interaction option to finish.

FinishInteraction(InteractableOption)​

Finishes an ongoing interaction with the specified interaction option. Invokes appropriate completion or cancellation events.

public void FinishInteraction(InteractableOption option)

Parameters​

NameTypeDescription
optionInteractableOptionThe interaction option to finish.

GetActiveInteractable()​

Gets the currently active interactable object based on proximity and visibility in the main camera's view. Updates the active interactable if a closer one is found.

public Interactable GetActiveInteractable()

Returns​

Interactable — The active Interactable if available; otherwise, null.

RegisterInteractable(Interactable)​

Registers an interactable object, adding it to the set of interactables currently in range, and logs the enlistment event with a timestamp.

public void RegisterInteractable(Interactable interactable)

Parameters​

NameTypeDescription
interactableInteractableThe interactable object to register.

StartInteraction(int)​

Starts an interaction with the currently active interactable using the specified interaction option index.

public void StartInteraction(int interactionIndex)

Parameters​

NameTypeDescription
interactionIndexintThe index of the interaction option to start.

StartInteraction(InteractableOption)​

Starts an interaction with the currently active interactable using the specified interaction option. Only starts if no other interaction is in progress.

public void StartInteraction(InteractableOption option)

Parameters​

NameTypeDescription
optionInteractableOptionThe interaction option to start.

TryGetActiveInteractable(out Interactable)​

Attempts to get the currently active interactable object.

public bool TryGetActiveInteractable(out Interactable interactable)

Parameters​

NameTypeDescription
interactableInteractableOutputs the active interactable if found.

Returns​

bool — true if an active interactable is found; otherwise, false.

UnRegisterInteractable(Interactable)​

Unregisters an interactable object, removing it from the set of interactables currently in range, and logs the delistment event with a timestamp. If the interactable is currently active and an interaction is in progress, forces the interaction to finish.

public void UnRegisterInteractable(Interactable interactable)

Parameters​

NameTypeDescription
interactableInteractableThe interactable object to unregister.

UpdateInteraction(int)​

Updates an ongoing interaction using the specified interaction option index.

public void UpdateInteraction(int interactionIndex)

Parameters​

NameTypeDescription
interactionIndexintThe index of the interaction option to update.

UpdateInteraction(InteractableOption)​

Updates an ongoing interaction with the specified interaction option. Invokes update events and handles auto-completion or cancellation conditions.

public void UpdateInteraction(InteractableOption option)

Parameters​

NameTypeDescription
optionInteractableOptionThe interaction option to update.

ActiveOptionsChanged​

Raised when the active interactable's option list changes, or when the active interactable itself changes, so the presenter can refresh the displayed prompts.

public event Action ActiveOptionsChanged

Event Type​

Action

InteractionCanceled​

Actions invoked when an interaction is aborted before completion.

public event Action<InteractionCommand> InteractionCanceled

Event Type​

Action<InteractionCommand>

InteractionCompleted​

Actions invoked when an interaction has successfully completed.

public event Action<InteractionCommand> InteractionCompleted

Event Type​

Action<InteractionCommand>

InteractionStarted​

Actions invoked when an interaction is initially started.

public event Action<InteractionCommand> InteractionStarted

Event Type​

Action<InteractionCommand>

Remarks​

The InteractionCommand being acted upon is passed as the argument so subscribers do not have to reach back into Command, which may already have been cleared by the time the handler runs.

InteractionUpdated​

Actions invoked during an ongoing interaction, typically each update frame.

public event Action<InteractionCommand> InteractionUpdated

Event Type​

Action<InteractionCommand>

  • Remarks
  • Properties
    • ActiveInteractable
    • Command
    • InteractableHashset
    • InteractionInProgress
  • Methods
    • Collect()
    • FinishInteraction(int)
    • FinishInteraction(InteractableOption)
    • GetActiveInteractable()
    • RegisterInteractable(Interactable)
    • StartInteraction(int)
    • StartInteraction(InteractableOption)
    • TryGetActiveInteractable(out Interactable)
    • UnRegisterInteractable(Interactable)
    • UpdateInteraction(int)
    • UpdateInteraction(InteractableOption)
    • ActiveOptionsChanged
    • InteractionCanceled
    • InteractionCompleted
    • InteractionStarted
    • InteractionUpdated