-
Trying to make a simple door open
however whenever a player collides with it, the door glitches out due to it being unanchored, but it is necessary to have the door unanchored for it to move.
[Video Example]
[OpenDoorScript] Located Inside the Door Mesh.
local door = script.Parent
local hinge = door.Hinge
local activationPart = door.Activation
local Angle = Instance.new("NumberValue") -- Store the angle in a value
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Back, Enum.EasingDirection.InOut, 0, false, 0)
local offset = hinge.CFrame:Inverse() * door.CFrame
local doorOpenLeft = tweenService:Create(Angle, tweenInfo, {Value = -75}) -- Left
local doorOpenRight = tweenService:Create(Angle, tweenInfo, {Value = -75}) -- 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
activationPart.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)
activationPart.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)
How would I go about fixing this issue? Is it as simple as changing a few settings on the Door?