Enjoy Simplicity : The Easy Way to Create Doors That Open

This is as easy as it comes in game development! We’re creating a door that opens using a trigger event! All we need is a door, some animations and an easy script to open and close the door when the right trigger has entered the area!

Setting Up The Scene

For this to work, we need a door! Feel free to use whatever door you’d like, but you can also use this free door asset found here.

For myself, I’m going to be using a door from the Sci-fi set that I purchased a while back found here!

Whatever door you choose, it won’t matter! This tutorial will work for any door you could think of. (Maybe not revolving doors).

The Components To Open Our Door

In order to open the door, we’ll need two distinct objects that have a few components attached to them.

The Door

Let’s start with the door since it’s already attached. In order to get the door to work, we need to add the following components. We first want to make sure that the door is a child of an empty game object. Since we’ll be animating the door later, we will want to attach the trigger collider onto the parent instead of the door object itself. Otherwise, the trigger collider will move when the door opens.

Trigger Door is my empty game object and I’ve added a Frame object as well, but this is optional if your door doesn’t have a frame or if you wish to keep the frame separate.

Next we need to select our empty Parent Object and add the following.

  •  Add a Box Collider
    • Is Trigger = True
    • Edit Colider (Expand and move the green cube until it fills the space in which you would like the door triggered)

 

This will be responsible for being the trigger when a player object enters it. It isn’t enough just to detect it however, we will also need to open it using an animation.

  • Add a Animator
  • Right click in Assets and create a Animation Controller named “Door”
  • Add the “Door” Animation Controller to the Animator
Now that we have our Animator and Animation Controller, we need two animation clips to open and close the door.
 
Select the Door Animation Controller and double click it to open it up in the Animator window.
  • Right click and Create New State called “Close”
  • Right click and Create a New State called “Open”
  • Right click on Close and make a new Transition to Open
  • Right click on Open and make a new Transition to Close
 

In order to get the transitions to work between the two states, we need some parameters. In the top left of the Animator window, we can choose the Parameters tab.

  • Hit the plus sign and add a new Trigger called “Open”
  • Hit the plus sign and add a new Trigger called “Close”

To wrap up the animation portion of this, we will need to create two animation clips. Navigate to the Animation Window and select the Parent Object of our door.

  • Create new clip called “Close”
    • Hit the red record button in the top left
    • Select the Door and move it a little bit and return it to its original position. (This will record the object in the closed position)
    • Stop the Record Button
  •  Create new clip called “Open” 
    • Hit the red record button in the top left
    • Select the Door and move it a little bit and return it to its original position. (This will record the object in the closed position)
    • Move the slider to 1:00 on the timeline and move the door to an open Position
    • Stop the Record Button

Find the Close and Open Animation Clips in your Assets folder, and make sure to TURN OFF Loop Time. Otherwise, your animations will loop once they’ve completed a full cycle.

In the Animator window, do the following.

  •  Make sure the States of Open and Close have the right animations associated with them (Motion)
  • Select Transition between Close to Open
    • Add Condition = Open
  • Select Transition between Open to Close
    • Add Condition = Close
 

To wrap up the door, we just need to add a script that detects when the player has entered/exited the trigger and animate based upon what has happened.

Add a new script and call it “Trigger Door” (or whatever you feel like calling it)

 

If you would like to easily download this code, you can do so on my Patreon!

The code here is pretty shrimple. We store the animator using the Start function.

We have OnTriggerEnter, which is triggered when a collider with a rigidbody enters it. We check the tag of the collider and if it’s equal to “Player”, we open the door.

We also have OnTriggerExit, which is called when the collider exits the trigger and closes the door.

The Player

The heavy lifting is done! This next one will be incredibly easy. We just need an object that represents our player with a few components on it.

Select your Player Object

  • Add a Box Collider (Or whatever collider you would like)
  • Add a Rigidbody
  • Set Tag = Player
 

Some important notes!

In order for the trigger event to work, one of the objects must have a rigidbody on it! Without it, Unity won’t trigger any physics events, which is how trigger events work.

Both objects can have their colliders set to trigger and still work!

The rigidbody can also be set to IsKinematic if you don’t want physics on whichever object you choose to have the rigidbody on.

Conclusion

That’s it! Super, quick and  easy. One of the best ways to open doors in life is through using triggers. I feel like that could be misenterpreted somehow. Oh well!