Open door when colliding?

You can write your topic however you want, but you need to answer these questions:

  1. I am trying to make it so that whenever I collide with a door/part it rotates about 90 degrees AROUND the HINGE

  2. I have a script ```
    local door = script.Parent – Assuming this script is a child of the door

local function onTouched(other)
local character = other.Parent
local humanoid = character:FindFirstChildWhichIsA(“Humanoid”)

-- If touched by a player's humanoid, rotate and tween the door
if humanoid then
    local hinge = door.Hinge.CFrame. + Vector3.new(-1,0,3) -- Assuming the hinge is at the center of the door
    local rotation = CFrame.Angles(0, math.rad(75), 0)  -- Rotate 90 degrees around the Y-axis
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false, 0)
    local tween = game:GetService("TweenService"):Create(door, tweenInfo, {CFrame = hinge * rotation})
    tween:Play()
end

end
door.Touched:Connect(onTouched)``` However it sorta works but doesn’t rotate correctly when I orientate the door in a different direction.

2 Likes

I tested this out, and it seems to work. I also added a close delay when the touch ends after 3 seconds.

local door = script.Parent
local hinge = door.Hinge
local Angle = Instance.new("NumberValue") -- Store the angle in a value
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new()
local offset = hinge.CFrame:Inverse() * door.CFrame
local doorOpenLeft = tweenService:Create(Angle, tweenInfo, {Value = 90}) -- Left
local doorOpenRight = tweenService:Create(Angle, tweenInfo, {Value = -90}) -- Right
local doorClose = tweenService:Create(Angle, tweenInfo, {Value = 0}) -- Close

local function getAngleBetweenVectors(vec1, vec2)
	return math.acos(vec1:Dot(vec2) / (vec1.Magnitude * vec2.Magnitude))
end

local function rotateDoor(angle)
	if angle > math.pi / 2 then
		doorOpenLeft:Play()
	else -- Calculate which way to open up
		doorOpenRight:Play()
	end
end

local function closeDoor()
	doorClose:Play()
end

door.Touched:Connect(function(touch)
	local humanoid = touch.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local playerFacingDirection = humanoid.Parent.HumanoidRootPart.CFrame.LookVector
		local doorFacingDirection = door.CFrame.LookVector -- Open door
		local angle = getAngleBetweenVectors(playerFacingDirection, doorFacingDirection)
		rotateDoor(angle)
	end
end)

door.TouchEnded:Connect(function(touch)
	local humanoid = touch.Parent:FindFirstChild("Humanoid")
	if humanoid then
		wait(3)
		closeDoor() -- Close door
	end
end)
game:GetService("RunService").Heartbeat:Connect(function(dt)
	door.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(Angle.Value), 0) * offset -- Smooth out the rotation
end)
1 Like

This works perfectly other than that it tries to close while I’m still in the door way. But for my use of it, it wont matter. Thank you very much for this!

1 Like

If it works well with you, that’s good.

How can I change the easing style to something like cubic?

Nevermind I just changed the variable “tweenInfo” to local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false, 0)

Sorry if I was late, I was AFK
EDIT: For me it is a bit buggy all the sudden. I am trying to fix that up a bit

EDIT: That was just me, and it cleared up

Do you have Touched function
You can also use HingeConstraint

He does have a touched function
image

Maybe this will help.
The hinges are a bit misplaced and the sounds probably won’t play, because of permissions, however, maybe this will help you with your door.

SwingingDoor.rbxl (108.2 KB)

image

local busy = false

local motorLeft = script.Parent:WaitForChild("LeftDoor"):WaitForChild("LeftPanel"):WaitForChild("HingeConstraint")
local motorRight = script.Parent:WaitForChild("RightDoor"):WaitForChild("RightPanel"):WaitForChild("HingeConstraint")
local trigger = script.Parent:WaitForChild("Trigger")
local closeSound = script.Parent.PrimaryPart:WaitForChild("CloseSound")
local openSound = script.Parent.PrimaryPart:WaitForChild("OpenDoor")

local speed = 10

local tweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Quart,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local tweenOpenRight = game.TweenService:Create(motorRight,tweenInfo,{TargetAngle=100})
local tweenOpenLeft = game.TweenService:Create(motorLeft,tweenInfo,{TargetAngle=100})
local tweenCloseRight = game.TweenService:Create(motorRight,tweenInfo,{TargetAngle=0})
local tweenCloseLeft = game.TweenService:Create(motorLeft,tweenInfo,{TargetAngle=0})

function CheckForClose()
	repeat
		local pass = false
		for _,i in pairs(game.Players:GetPlayers()) do
			if i.Character and i.Character.PrimaryPart then
				if (i.Character.PrimaryPart.Position - trigger.Position).Magnitude < math.max(math.max(trigger.Size.X,trigger.Size.Y),trigger.Size.Z)/2+5 then
					pass = true
					break
				end
			end
		end
		wait()
	until (pass == false) 
end

trigger.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and busy == false then
		busy = true
		closeSound:Stop()
		openSound:Play()
		trigger.BrickColor = BrickColor.Green()
		tweenOpenRight:Play()
		tweenOpenLeft:Play()
		tweenOpenRight.Completed:Wait()
		CheckForClose()
		openSound:Stop()
		busy = false
		trigger.BrickColor = BrickColor.Red()
		tweenCloseRight:Play()
		tweenCloseLeft:Play()
		spawn(function() wait(.5) closeSound:Play() end)
		tweenCloseRight.Completed:Wait()
		--closeSound:Play()
	end
end)

1 Like