How to rotate part along an attachment

I want to rotate a door along an attachment
image

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"))

Use pivots. Set the pivot of the door model to the attachment’s CFrame then use PivotTo to change the CFrame by the desired angle.

1 Like

Another method is to use CFrames

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.

2 Likes

I can’t use Pivots, as all objects are parts. Changing it to models now would be a huge hassle to have to recode a ton of the back work :confused:

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
image

Placed in game
image

When I open them
image

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.

I feel that over complicates it. And I said I didn’t want hinges or anything, as I want the door to snap

HingeConstraints will snap if you crank the AngularSpeed and the AngularResponsiveness up.

It isn’t that complicated at all. Try taking a copy of the model and see.
You can’t learn if you don’t try.

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.

1 Like

How can I set the pivot point via code?? I can’t find any documentation on it?

PivotOffset.

2 Likes

Unsure what I am doing wrong :confused: Clicking on it just raised the door

image

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 :confused:
image

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

2 Likes

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.

Have a read through this tutorial:
https://developer.roblox.com/en-us/articles/Understanding-CFrame