Join Jesse Freeman for an in-depth discussion in this video The door's logic, part of Unity 5: 2D Level Design.
- [Instructor] With the door animation working, it's time for us to wire this up with code. But before we do that, let's go ahead and clean up the door game object. Start by selecting it from the Hierarchy, and in the Sprite Renderer, let's give it the correct sprite, so that when we look at the door and lay it out in our map, it's a little bit easier to visualize. Let's select the sprite picker and go to door-00. Next, you'll see that the door might not be showing up on top of the background layer.
Let's go ahead and change the Order in Layer to five. This'll be even higher than the player, so that when the player walks through it, if any bit of the door is showing, the player will appear behind the door. Next, we're going to want to add a Box Collider to keep the player and anything else from walking through the door when it's closed. Let's select the Box Collider 2D. And finally, inside of our Jetroid level design folder, let's create a new folder.
We'll call this Prefabs and let's drag our door into the Prefab folder. Now we're ready to start coding up our door. Going back into the root of the Jetroid level design folder, create one more folder, and call this Scripts. Inside of this folder, let's create a C# Script called Door. And then let's open this up in MonoDevelop. Our door's going to need a reference to the animator, and to the box collider.
At the top, let's add two new fields, the first being the Animator, and the second being the BoxCollider2D. We'll call this collider2D. Next, inside of our start method, let's set values for both of these fields. We'll set the value of the animator equal to the component of the animator on the game object. We'll set collider2D equal to the BoxCollider2D component as well.
Now, let's create two methods to enable and disable our BoxCollider2D. The first method will have a return type of void, and we'll call this EnableCollider2D. Here we'll set the collider2D's enable value to true. Let's copy this method and paste it below. We'll rename EnableCollider to DisableCollider and set the value to false.
Now let's go ahead and create our first public method to open the door. It'll have a return type of void, and we'll call this Open. We're going to want to tell the animator to set an integer value for the AnimState to equal one, which will play the open animation. But when we close the door, we actually want to have a slight delay. This way the player has a chance to get away from the door as it's closing.
First, we're going to need a way to keep track of the delay value. We'll create a public float, and we'll call this closeDelay. We'll set this equal to .5f. Now, we'll need to create our close method. We'll make this public with a return type of void, and we'll call this Close. In order for us to use the delay, we're going to need to start a coroutine.
Once we set up the coroutine, let's go ahead and call a method CloseNow, which we haven't created yet. The coroutine looks for an IEnumerator method in order for it to call at a certain interval. What we're going to do is create a private method with a return type of IEnumerator, and we're going to set this to CloseNow. Before we actually close the door, let's go ahead and use a yield return statement and create a new WaitForSeconds and pass in the closeDelay.
And now, when we call close, the coroutine will wait for the closeDelay and afterwards, let's call the animator.SetInteger and we'll set the AnimState value equal to two to show the close animation. Let's go ahead and save our code and go back into Unity. We're going to want to drag the door script over to the door. You'll also notice, we have a little bit of a warning here.
The warning is telling us that we're using the same variable name as an inherited member. In essence, it's hiding the child class's member, collider2D used to be built into the framework but it's now being deprecated. Meaning, it can be removed at any time but since it still exists, the compilers going to want us to use the new keyword in front of it. So, let's double click on the warning which will open up our code and here, we'll simply use new.
If we save our code and go back into Unity, when it recompiles, the warning goes away. Let's go ahead and save the changes we've made to the door and now, we have all the code we need to control the door.
- Importing assets
- Building a level
- Adding collisions and layers
- Populating the level
- Creating and triggering doors
- Creating switches and gizmos
- Adding different enemies
- Building a UI for an air meter timer