In almost every game we see some kind of door or blocked which player needs to open with key or another way. We are going to make a simple key door which will open when player will press 'E' key.
Start with a new project. Delete everything and save the project as 'KeyDoor.blend'.
Now create a plane mesh - go to - 'Add->Mesh->Plane'.
Scale the plane a little bit so that we have some grounds to move the player.
Now add a cube to make the door - go to 'Add->Mesh->Cube'.
Scale and move the cube as you like to make a door.
I am going to use a rotating door. So, we have to change the pivot point of the door to make it rotate like a door. Go to edit mode and press 'Ctrl+A' to select all and move the door on Y axis.
Press 'Tab' to exit edit mode.
You can add some more cubes and make it walls at both side of the door, if you want.
I am going to use action, rotate animation, to open and close the door.
Select the door - go to 'Timeline Editor' - add a key on frame 1 when it is closed. To add a key press 'I' and select 'Rotation' from the pop up menu.
Move the playhead to frame 30, now rotate the door as you like and insert a key.
Now move the playhead to frame 60 and rotate the door back to its close position and insert a key frame. You can use 'Dopesheet Editor' to copy and paste the frame 1 to frame 60, because both are same position.
Door still selected, open the 'Logic Editor'. Add two 'Message' sensors, add two 'And' controller and add two 'Action' actuators.
Insert 'open' in the first message sensor's subject, insert 'close' in second message sensor's subject. In action actuators, select the 'CubeAction' in value input for both the actuators. In first action set the start frame to 1 and end frame to 30. In second actuators set the start frame to 30 and end frame to 60.
Note: You can change the action name in the 'Action Editor'.
We are done with door. It is ready for the action. Save your project.
I am not going to make a complete player object. For this example I am going to make a simple player who only moves forward and backward. Make the player as you like.
Open the 'Logic Editor', select your player and add a property - 'player'. Type does not matter here.
Add some movement logic to the player as you like.
Add a cube, scale - position it in front of the door. From now on I will call it door guard :).
Go to 'Physics' panel and change the physics type to 'Sensor'.
Set the physics visibility to Invisible. Click the 'Invisible' button.
That door guard still selected, open 'Logic Editor'. Add a property, name it 'open', change its type to boolean.
Add a 'Collision' sensor. Add a Keyboard sensor. Add a 'Python' controller and add two 'Message' actuator. Name them as you like or leave as it is.
In the collision sensor, add 'player' in the 'Property' input. Now it will only check for the objects which has 'player' property in it. In the keyboard insert 'E' in 'Key' input and click the 'Tap' button. In the controller select the python script( I name it 'KeyDoor.py', see below for the script ). With the actuators, select the door in the 'To' input from the drop down menu for both the actuators.
Add 'open' in first actuator and add 'close' in second actuator in the 'Subject' input field.
Now open the 'Text Editor' and create a new text. Name it 'KeyDoor.py( as mentioned above ).
Here is the python code for the KeyDoor.py
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
#Sensors
coll = cont.sensors["Collision"]
key = cont.sensors["Keyboard"]
#Actuators
open = cont.actuators["Message"]
close = cont.actuators["Message1"]
if coll.positive:
#do something here,
#like pop a message - "Press E to open the door"
if key.positive:
own["open"] = not own["open"]
if own["open"]:
cont.activate(open)
else:
cont.activate(close)
Press 'P' and enjoy.
Here is the finish .blend file - KeyDoor.blend
Start with a new project. Delete everything and save the project as 'KeyDoor.blend'.
Now create a plane mesh - go to - 'Add->Mesh->Plane'.
Scale the plane a little bit so that we have some grounds to move the player.
Now add a cube to make the door - go to 'Add->Mesh->Cube'.
Scale and move the cube as you like to make a door.
I am going to use a rotating door. So, we have to change the pivot point of the door to make it rotate like a door. Go to edit mode and press 'Ctrl+A' to select all and move the door on Y axis.
Press 'Tab' to exit edit mode.
You can add some more cubes and make it walls at both side of the door, if you want.
I am going to use action, rotate animation, to open and close the door.
Select the door - go to 'Timeline Editor' - add a key on frame 1 when it is closed. To add a key press 'I' and select 'Rotation' from the pop up menu.
Move the playhead to frame 30, now rotate the door as you like and insert a key.
Now move the playhead to frame 60 and rotate the door back to its close position and insert a key frame. You can use 'Dopesheet Editor' to copy and paste the frame 1 to frame 60, because both are same position.
Door still selected, open the 'Logic Editor'. Add two 'Message' sensors, add two 'And' controller and add two 'Action' actuators.
Insert 'open' in the first message sensor's subject, insert 'close' in second message sensor's subject. In action actuators, select the 'CubeAction' in value input for both the actuators. In first action set the start frame to 1 and end frame to 30. In second actuators set the start frame to 30 and end frame to 60.
Note: You can change the action name in the 'Action Editor'.
I am not going to make a complete player object. For this example I am going to make a simple player who only moves forward and backward. Make the player as you like.
Open the 'Logic Editor', select your player and add a property - 'player'. Type does not matter here.
Add some movement logic to the player as you like.
Add a cube, scale - position it in front of the door. From now on I will call it door guard :).
Go to 'Physics' panel and change the physics type to 'Sensor'.
Set the physics visibility to Invisible. Click the 'Invisible' button.
That door guard still selected, open 'Logic Editor'. Add a property, name it 'open', change its type to boolean.
Add a 'Collision' sensor. Add a Keyboard sensor. Add a 'Python' controller and add two 'Message' actuator. Name them as you like or leave as it is.
In the collision sensor, add 'player' in the 'Property' input. Now it will only check for the objects which has 'player' property in it. In the keyboard insert 'E' in 'Key' input and click the 'Tap' button. In the controller select the python script( I name it 'KeyDoor.py', see below for the script ). With the actuators, select the door in the 'To' input from the drop down menu for both the actuators.
Add 'open' in first actuator and add 'close' in second actuator in the 'Subject' input field.
Here is the python code for the KeyDoor.py
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
#Sensors
coll = cont.sensors["Collision"]
key = cont.sensors["Keyboard"]
#Actuators
open = cont.actuators["Message"]
close = cont.actuators["Message1"]
if coll.positive:
#do something here,
#like pop a message - "Press E to open the door"
if key.positive:
own["open"] = not own["open"]
if own["open"]:
cont.activate(open)
else:
cont.activate(close)
Press 'P' and enjoy.
Here is the finish .blend file - KeyDoor.blend


























