Interactions 103: Triggers

In the previous tutorials, we created a button that opens a door - and had that door close after a set time:

But what if the Player is underneath it when it shuts? In this tutorial, we'll have the door detect the Player's presence - and remain open if they're too close. To achieve this, we'll make use of a Trigger.

We can create a new Trigger by clicking the Trigger in the Scene Manager's list of logic prefabs. It is represented in the scene by a red box. Reposition it so that it sits underneath the door:

Next, parent it to the Door GameObject. That way, when the door opens, the Trigger will move up with it - and occupy the empty doorway.

Triggers can be made to run Actions when the Player enters them - but we'll want the Actions for this Trigger to run in the background, so that it doesn't take control away from the player. We can configure this in the Trigger's Inspector, by setting its When running property to Run In Background:

As for its Actions: we can configure them either beneath this list of properties, or by clicking its Action List Editor button.

In the last tutorial, we created a Cutscene that closes the door once opened, and that Cutscene has a delay on it. In this Trigger's Actions, we'll restart this delay by "killing" the Cutscene and then re-running it.

To do this, change the Trigger's Action to the type ActionList: Kill, and assign Close door in its ActionList field:

Finally, change the Action's After running field to Run Cutscene, and assign the same thing in the Cutscene to run field:

Now, when the Player walks through the doorway, it'll set off the Trigger - and restart the delay to the Close door cutscene.