From the course: Unity 5: 2D Level Design

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Trigger the door

Trigger the door - Unity Tutorial

From the course: Unity 5: 2D Level Design

Start my 1-month free trial

Trigger the door

- [Instructor] The last thing we want to do is add a trigger to let the door open and close when the player gets near it. Let's go into our Jetroid level design folder and inside of Scripts, create a new C# script. We're going to call this script Door Trigger. Then let's open this up in mono develop. In order for the Door Trigger to work, we're going to need a reference to the door we want to control. At the top of the class, let's create a new field tight to the door class and we'll simply call this door. Next, we can remove the start and update methods since we're not going to be using them. Instead, let's create a new event handler for a trigger. We'll have the return type of void. We'll call this OnTriggerEnter2D. We'll need to pass in a Collider2D as the type and we'll call this target. Next, we want to test if the target.gameObject.tag == "Player". We want to make sure that the door only opens when the player gets near it so we'll look for the player's tag. If the player is…

Contents