How to open a door with a key and GUI alerts in unity 3D. (using GUI elements and static variables)

Posted: March 3, 2011 in Tutorials

Hi again! this tutorial is for the software unity3D and it let you interact with the door and the key in the scene. in here we will teach:

if you pick up the key you can open the door by pressing “E” button (with the GUI alert to inform the player) and if you don’t have the key you cannot open the door and it will be the GUI come out tell you to collect the key.

You can download the whole unity package that I test on it from here and see the process. (It is package so just import it to one unity file and use it as package).

1- Create necessary objects for your scene which you want to open the door with and etc.What I created here is a “Cylinder (as a key)”, “2 Cube (1 for door and the other one for floor)”, “first person controller (As a player)” and one light.

2- Select your door and create an empty object and call it as door.

3- Select that empty object and make the “Box Collider” with it. (Components> Physics> Box Collider) and adjust the size of the trigger bigger than the door itself. And make the door children of this empty game Object. (drag the cube of the door and drop it to that empty game object)

4- Select your key and after making the “Key” tag from tag manager, assign it to the key object.(Edit> Project setting> Tags)

5- Select your key and make sure that it has a collider and it’s selected already.

6- Make one Empty object on your key or anywhere in the scene for assigning the GUI Script on it. Start scripting on your GUI it means that this GUI when you get the key it will be activated and this GUI of the key will appear. It has one static var because we want to call it after we picked up the key not whenever we can see it. And it has one var of texture 2D which allow us to assign the chosen picture for the key. Name your script “GUIKey” and assign it to that empty game object.

7- Assign the key vector or any GUI picture that you have chosen by drag and drop it in the inspector panel.

8- Alright, In here we will start scripting on our door. In function update we will say how the door must open and close. And in the function of “OnTriggerEnter” or “OnTriggerExit” we will define the variables which must be true or not. As you can see we called our previous script with its static variable from here. Also we must make another script for the door that if its enter the trigger it will say that press E to open the door or if you don’t have the key go and find the key. This one you can see it in the next step (Step 9).

9- Alright in this step you know what we want to do; we want to make the script for alerting the GUI and make it something like alert. As you saw in previous script we defined another static variable which we are calling it from this script to show the script in the right and exact time. Also I made a print then you can see it from here what the process is and what we are doing.

10- Alright in this step we want to make the script that if the player get the key the GUI that we defined first appear and also he can open the door with it. And also destroy the key. We used our two static variables here and connect our script to another 2 scripts. (assign it to your Player)

11- Don’t forget to tag your first person controller, user or anything as “Player” the player tag does already exist there. Because we told in the script if an object with the tag of “Player” enter the trigger do anything so if you don’t pay enough attention to the tags those scripts won’t run as well.

12- In this step we will assign the scripts that we did before to the objects that we need. We will assign that script of alert for press E or anything with the script of opening the door to the empty game object of the door which that door is the children of it. (Don’t assign the scripts to the door itself; assign them to the empty game object which is the parent of the main door). Also make sure that the numbers of smooth and door open Y in the inspector panel is not 0.

13- Play the game and enjoy it!

P.S: I made those scripts just for opening the door so for close the door also you can make the script that if the player go out of the trigger bring the door to the first position.

You can download the whole unity package that I test on it from here and see the process. (It is package so just import it to one unity file and use it as package).

Comments
  1. Lunk says:

    Thank you for this tutorial, I go test it now!!

Leave a comment