Interaction System

Checked with version: 2017.3

-

Difficulty: Beginner

The interaction system allows the player to interact with GameObjects. To do this, it uses three components:

  • InteractOnCollision2D

  • InteractOnTrigger2D

  • InteractOnButton2D

Interactions are dependant on GameObjects having a Collider 2D Component on them. A Collider is an invisible area around a GameObject that can detect when something touches or enters it. The main ones we use in the Kit are Box (Box Collider 2D), Capsule (Capsule Collider 2D) and Circle (Circle Collider 2D).

These are all set up for you in the various Prefabs provided in the Kit.

If you wish to add new types of interactions on GameObjects or create your own, depending on which of the following InteractOn components you use, you must take note of the Is Trigger checkbox on the Collider of the GameObject you’d like to make interactable. This documentation includes information on what state the Collider needs to be in for a particular InteractOn component.

Interact On Collision 2D

This is used to trigger events when GameObject Colliders touch.

  • Script: The name of the script being used (cannot be changed).

  • Layers: Only GameObjects on the selected Layers trigger the events.

  • On Collision: Events listed here happen when a GameObject that belong to the set layer collide.

(If setting up your own, there is no need to enable Is Trigger on the Collider)

Interact On Trigger 2D

This is used to trigger events when a GameObject enters a collider and more events when an GameObject exits a collider.

(If making your own, ensure Is Trigger is enabled on the Collider Component).

  • Script: The name of the script being used (cannot be changed).

  • Layers: Only GameObjects on the selected Layers trigger the events.

  • On Enter: Events listed here happen when a GameObject that belong to the set layer enter the Trigger.

  • On Exit: Events listed here happen when an GameObject that belongs to the set layer exits the Trigger.

  • Inventory Checks: This allows you to ensure events only happen when a GameObject entering the trigger has a GameObject in its inventory (or example, only opening a door once a player entering the trigger zone has a Key in their inventory).

    • Size: Increase this number to increase the amount of Inventory the trigger should expect. This is set to 0 by default.

    • Element 0: The first Inventory Check.

      • Inventory Items: GameObjects it expects in the inventory of the GameObject colliding with it.

        • Size: The number of items you would like the triggering GameObject to carry in its inventory.

        • Element 0: The first inventory item needed. This must match the expected names, these are listed in the Inventory System section.

  • On has Item: Events triggered when the GameObject holds ALL the items set in the Inventory Items.

  • On Does Not Have Item: Events triggered when the GameObject does not hold ALL the items set in Inventory Items.

Interact On Button 2D

This is used to trigger events when a GameObject is inside the trigger zone and a player presses the Interact button in the game (see the Player Input section for more information on the Interact Button).

(If making your own, ensure Is Trigger is enabled on the Collider Component).

  • Script: The name of the script being used (cannot be changed).

  • Layers: Only GameObjects on the selected Layers will trigger the events.

  • On Enter: Events listed here happen when an GameObject that belongs to the set layer enters the Trigger.

  • On Exit: Events listed here happen when a GameObject that belongs to the set layer exits the Trigger.

  • Inventory Checks: This allows you to ensure events only happen when a GameObject entering the trigger and pressing the interact button has a GameObject in its inventory (for example, only opening a door once a player has a Key in their inventory when entering the trigger and pressing the interact button).

    • Size: Increase this number to increase the amount of Inventory the trigger should expect. This is set to 0 by default.

    • Element 0: The first Inventory Check.

      • Inventory Items: GameObjects it expects in the inventory of the GameObject colliding with it.

        • Size: The number of items you would like the triggering GameObject to carry in its inventory.

        • Element 0: The first inventory item needed. This must match the expected names listed in the Inventory System section.

  • On Button Press: Events that are triggered when the interact button is pressed.

Related tutorials