PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Top Down Action: Keys and Doors

Top Down Action: Keys and Doors

Jul 30 2018, 2:20 PM PST 5 min

Our game has several locked doors to encourage exploration and to pace the player’s progression. These doors will open when their associated key-cards are found by a player.

In the Workspace, under the LevelGeometry folder, there are three folders named Door. Each contains two parts: one for the door itself, and one for the key to unlock the door. Insert a Script into each Door folder; These scripts will be used to open the door when the key is touched by a player.

TDA_Key_Explorer.png

All of these scripts will contain the same code, so we should link them together. When you link scripts together, changing one will change all of the scripts linked to it; This makes managing code much easier. To setup the link, right click on one of the scripts and select Create new LinkedSource. Name the BaseScript/LinkedSource|LinkedSource Door Script. Keep in mind that you will have to publish your game before you can create a BaseScript/LinkedSource|LinkedSource.

Select the scripts we inserted into the other doors and change their BaseScript/LinkedSource|LinkedSource property to the Door Script we just made.

TDA_Key_LinkedSource.png

Now the scripts are linked, and if we make a change to one of them all of the others will automatically update.

The first thing to do is to remove the key when a player touches it. Enter the following code into one of the door scripts:

local key = script.Parent.Key

local function onKeyTouch(otherPart)
	if game.Players:GetPlayerFromCharacter(otherPart.Parent) then
		key:Destroy()
	end
end

key.Touched:connect(onKeyTouch)

BasePart/Touched|Touched is an event that fires whenever a part comes in contact with another part. The function connected to a touched event is passed the contacted part as an argument. In this case, we are checking if the parent of the contacted part is part of a player character. If so, then we know that a player came in contact with the key in which case we can remove it.

Opening the Door

To open the door we will use another BodyMover called a BodyPosition. We already used another BodyMover, BodyGyro, when rotating the character. A BodyPosition works similarly, but instead of trying to reach a goal rotation, a BodyPosition tries to move a part to a goal location.

local key = script.Parent.Key
local door = script.Parent.Door

local function onKeyTouch(otherPart)
	if game.Players:GetPlayerFromCharacter(otherPart.Parent) then
		key:Destroy()
		door.Anchored = false
		door.CanCollide = false
		door.BodyPosition.Position = door.Position + Vector3.new(0,10,0)
		door.Transparency = 0.5
	end
end

key.Touched:connect(onKeyTouch)

When a player touches the key, we first unanchor the door. This allows the door to be moved by physics. If we left the door anchored, no amount of force would be able to move it. Next we turn off collisions with the door. This allows the player to move through it without stopping and also allows the door to pass through the walls around it. We then set the goal position of the door’s BodyPosition to 10 studs above the current position of the door. We also make the door slightly transparent so that we can see through it when the player moves to the other side.

Playing a Sound

To make the game more immersive, we’ll play a sound when the door opens. Each door in this game has a sound included which we can easily play from the script.

local key = script.Parent.Key
local door = script.Parent.Door

local function onKeyTouch(otherPart)
	if game.Players:GetPlayerFromCharacter(otherPart.Parent) then
		key:Destroy()
		door.Anchored = false
		door.CanCollide = false
		door.BodyPosition.Position = door.Position + Vector3.new(0,10,0)
		door.Transparency = 0.5
		door.Sound:Play()
	end
end

key.Touched:connect(onKeyTouch)

TDA_Key_DoorOpening.png