Doors That Swing
Lesson 3 of 25 in Coddy's Roblox Game: Escape the Dark course.
The lobby's door has a ProximityPrompt: near it, the player sees "E Open" (a tap on a phone), and pressing it fires the prompt's Triggered with the player.
A door turns on its hinge, not its middle. The door model has an invisible Hinge part at its edge. Where the panel is, measured from the hinge, is hinge.CFrame:ToObjectSpace(panel.CFrame); turn the hinge a quarter turn and put that back on, and you have the open door:
local goal = hinge.CFrame * CFrame.Angles(0, math.rad(90), 0) * hinge.CFrame:ToObjectSpace(panel.CFrame)
TweenService:Create(panel, TweenInfo.new(0.6), { CFrame = goal }):Play()A tween makes it swing instead of jump. Once open, the door's Open attribute is set and its prompt switched off.
Challenge
EasyMake the lobby door swing open.
- Get TweenService. Write
openDoor(door): set itsOpenattribute to true and switch offdoor.Panel.Prompt. - Then tween
door.Panelfor 0.6 seconds to the body'sgoal, a positive quarter turn:math.rad(90). - Write
setupDoor(door):door.Panel.Prompt.Triggeredopens it unless it is open. Call it forworkspace.Rooms.Lobby.Door.
Press Play and press E at the door: it swings away from you. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Lighting = game:GetService("Lighting")
local Settings = require(game.ReplicatedStorage.Settings)
-- Night falls: no sun, a little light everywhere, and fog
Lighting.ClockTime = Settings.ClockTime
Lighting.Ambient = Settings.Ambient
Lighting.OutdoorAmbient = Settings.Ambient
Lighting.FogColor = Color3.new(0, 0, 0)
Lighting.FogEnd = Settings.FogEnd
print("Lights out")
-- Lesson 3: Doors That Swing goes here
-- Every player: a flashlight every life
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local flashlight = ServerStorage.Flashlight:Clone()
flashlight.Parent = player.Backpack
local beam = flashlight.Handle.Beam
flashlight.Activated:Connect(function()
beam.Enabled = not beam.Enabled
end)
end)
end)
All lessons in Roblox Game: Escape the Dark
Practice on your own: Roblox playground