Tutorial: Doors - CodingDino/Dino-Unity-Toolkit GitHub Wiki

In this tutorial, you will learn how to link two levels together with a "door". The door can be anything - it doesn't have to be an actual door. It might be an item, portal, cliff, pipe, or anything at all - but we'll call it "door" in this tutorial.

This tutorial assumes you already have the two levels created, and have a move-able character with a collider attached. You will need a copy of your character at the starting point in both levels.

Before we start, let's clearly state our goal behaviour:

When the player touches the door, move the player to the next level.

Setup

First, lets create our door. Drag the sprite for your door into the scene. This will be the GameObject we'll be using for the rest of the tutorial.

Action

In our goal above, the Action was:

... move the player to the next level.

Our level is a separate scene. To change scenes, we need a SceneAction component. Select the door GameObject and add a new SceneAction component to it.

We will use ActionLoadScene() to load our new level, but as always, we need to trigger this with an Activator.

Activator

The Activator for our door from our goal above is:

When the player touches the door...

If you see "touches" in any goal you should recognise this means collision. Let's add a collider to our door - a BoxCollider2D should be fine. Select your door GameObject and add a BoxCollider2D component to it.

Now it can collide with the player, but we still need our Activator to trigger the Action. Add an OnCollision2DActivator to the door as well.

For this Activator, we need to set up some conditions - we wouldn't want to load the next level if any old thing bumped into our door. What if an enemy hit it? No good!

Instead, check the "Check Tag" box and type in "Player" into the text box. This will check any GameObject that bumps into the door and check if that GameObject has the "Player" tag.

Now select the player's GameObject and make sure it has the "Player" tag selected - the tag is displayed in the top left part of the Inspector window.

Finally, in the OnCollission2DActivator, add a new action to the OnCollisionEnter action list. Drag in the Door (that's where the action we want to use is located). Select the SceneAction component, and select ActionLoadScene(). Finally, type in the name of your next level (such as "Level2") into the box. This must match the spelling and capitalisation of your scene's name exactly in order to work.

The Action and Activator are all set up, but SceneActions won't work if the scenes are not added to the build settings. If you haven't already done so, add these two scenes to the build settings for your project by selecting File > Build Settings and dragging your scenes in to the scene list in the popup window.

Testing and Further Development

Try it out - your game should now load the next scene when you player touches the door.

If you want to only unlock the door when certain items have been picked up the player, you can use an EnableObjectAction to only turn on the door when an item has been picked up. See more info in Tutorial: Keys.