I don’t want to use a hinge or anything like that, as the door should only open when clicked on and there will be no opening animation or anything. Atm I rotate the attachment, but I can’t figure how to make the door follow
local Door = Block:FindFirstChild("Door")
local Attachment = Door:FindFirstChildWhichIsA("Attachment")
local Rotation = Block:GetAttribute("Open") and Vector3.new(0, 0, 0) or Vector3.new(0, -90, 0)
Attachment.Orientation = Rotation
Block:SetAttribute("Open", not Block:GetAttribute("Open"))
off = CFrame.new(Attachment.Position) -- Get offset from door
rot = Vector3.new(0,0,0) -- Rotation to apply (in degrees)
CF= CFrame.new(Attachment.WorldPosition) * -- Get position of attachment in world
CFrame.Angles( -- Apply the rotation
math.rad(rot.X),
math.rad(rot.Y),
math.rad(rot.Z)
) *
off:inverse() -- Apply offset of attachment -> door
Door.CFrame = CF
Multiplying CFrames (A*B), applys B as an offset to A. So first you find where the world position of the attachment is. Then apply the rotation, which rotates it in place. Then apply another offset from the door and attachment.
Since the attachment is relative to the door (which how we set ‘off’), we need to invert it. The above code assumes the attachment is parented under door, otherwise the position property isn’t local to the door.
This seems to come close. How can I make it rotate relative to what the doors rotation is based on player place
local Door = Block:FindFirstChild("Door")
local Attachment = Door:FindFirstChildWhichIsA("Attachment")
local ClosedRotation = Attachment.Orientation
local OpenRotation = ClosedRotation + Vector3.new(0, -90, 0)
Block:SetAttribute("Open", not Block:GetAttribute("Open"))
local Offset = CFrame.new(Attachment.Position)
local Rotation = Block:GetAttribute("Open") and OpenRotation or ClosedRotation
local RotatedCFrame = CFrame.new(Attachment.WorldPosition) *
CFrame.Angles( -- Apply the rotation
math.rad(Rotation.X),
math.rad(Rotation.Y),
math.rad(Rotation.Z)
) *
Offset:inverse()
Door.CFrame = RotatedCFrame
It’s kinda wonky on some, where it rotates outside of the red box, so I want it to rotate like these
Why can’t you use a HingeConstraint?
I made a door for a guy a while ago to teach how it’s done.
Mine uses a Touched event on a Part below the door, but you could easily put the ClickDetector in the door itself and script that.
From the looks of it you have a lot of Parts in that door. I’d make them all CanCollide and CanTouch false, but put 1 large Transparent CanCollide Part as the actual door and just weld them all together.
You can still use pivots without models. My initial assumption was that you were using models. I’m not sure why you aren’t but even in the case of not using a model and only using parts pivots are still your best friend. You can have all the parts use the attachment as a pivot point and then simply apply a rotation so they rotate around the pivot.
Unsure what I am doing wrong Clicking on it just raised the door
local Door = Block:FindFirstChild("Door")
local Attachment = Door:FindFirstChildWhichIsA("Attachment")
local ClosedRotation = CFrame.new(0, 0, 0)
local OpenRotation = CFrame.new(0, math.rad(90), 0)
Block:SetAttribute("Open", not Block:GetAttribute("Open"))
local desiredPivotCFrameInWorldSpace = Attachment.CFrame
Door.PivotOffset = Door.CFrame:ToObjectSpace(desiredPivotCFrameInWorldSpace)
Door:PivotTo(Door:GetPivot() * (Block:GetAttribute("Open") and OpenRotation or ClosedRotation))
EDIT
Further testing, the pivot point is being set weirdly anyway
You’re probably going to want to keep pivots in object space and not change over to world space assuming this is for a block placement system. PivotOffset is already in object space so you can apply an angled CFrame on whatever GetPivot returns and then set the pivot to that.
Here’s a quick repro with code I wrote up:
local Block = script.Parent
local Door = Block.Door
local Attachment = Door.Attachment
local CLOSED_CFRAME = CFrame.Angles(0, math.rad(90), 0)
local OPEN_CFRAME = CFrame.Angles(0, math.rad(-90), 0)
Door.PivotOffset = Attachment.CFrame
while true do
local inverseCurrentState = not Block:GetAttribute("Open")
Block:SetAttribute("Open", inverseCurrentState)
if inverseCurrentState == true then
-- Door should be opened
Door:PivotTo(Door:GetPivot() * OPEN_CFRAME)
else
Door:PivotTo(Door:GetPivot() * CLOSED_CFRAME)
end
task.wait(1)
end
Well, if your door is moving, but not rotating then your aren’t doing the CFrame correctly.
In your code you use local ClosedRotation = CFrame.new(0,0,0)
That’s a Postional CFrame, not a Rotational one.