# How to rotate a part after another part's local space?

Yes, I know it’s my 4th topic about CFrames in less than one week. I’m sorry but I’m still confused about world space and object space!

So, here’s exactly what I’m trying to ahieve:
There are 2 parts of identical size but different position: Part0 and Part1. You know the keybind ctrl+L that allows you to switch from world space to local space, right? The Part1 must rotate itself on its local axis after the Part0’s local axis.
Since it’s, extremely hard to explain, I’ll give an example:

• If the Part0 is rotated 90 degrees on its local X axis, the Part1 should automatically rotate on it’s own local axis by 90 degrees.

My current script doesn’t exactly works like explained above, because I’m not very familiar with world space/local space and I don’t really know what’s missing.

``````local originalCF = p0.CFrame --The Part0's original CFrame.
local function onOrientationChanged()
local newCF = part0.CFrame --Save the new Part0's CFrame.
local Angles = (newCF - newCF.p) * (originalCF - originalCF.p):Inverse() --newCF / originalCF

part1.CFrame = part1.CFrame * Angles
originalCF = newCF --Apply the new Part0's CFrame.
end
``````

I’ll try to make a video if what I wrote is too hard to understand

1 Like

I’m not sure what you mean. Can’t you just multiply them both by the same rotation cframe because that rotates about their local axis?

``````local NewRotation = CFrame.Angles(0, math.pi / 2, 0)

Part0.CFrame = Part0.CFrame * NewRotation
Part1.CFrame = Part1.CFrame * NewRotation
``````

Both parts will rotate 90 degrees relative to where they already are?

I’m not sure what you mean, can you make a graphic of some sort.

1 Like

Maybe with this pseudo-code, it’ll b easier to understand what I’m trying to achieve?

``````if (part0 was rotated by n degrees on its relative X axis) then
part1.CFrame = part1.CFrame * CFrame.Angles(n, 0, 0)
end
``````

I have no idea how to detect when the part0 was rotated by n degrees on its relative X axis, that’s the problem.

Are you trying to do something like this?

``````local Part0 = workspace.Part0
local Part1 = workspace.Part1

local NewRotationForPart0 = CFrame.Angles(0, math.pi / 2, 0)
local Part0Old = Part0.CFrame - Part0.Position

Part0:GetPropertyChangedSignal("CFrame"):Connect(function()
local X1, Y1, Z1 = Part0.CFrame:ToEulerAnglesYXZ()
local X2, Y2, Z2 = Part0Old:ToEulerAnglesYXZ()

local Difference = CFrame.Angles(
X1 - X2,
Y1 - Y2,
Z1 - Z2
)

Part1.CFrame = Part1.CFrame * Difference
Part0Old = CFrame.Angles(X1, Y1, Z1)
end)

Part0.CFrame = Part0.CFrame * NewRotationForPart0 --// Apply a rotation N to Part0
``````

Here is an example of me rotating Part0 on my own.

3 Likes

Here are 3 images to show exactly what I’m trying to achieve.

Note: There is a Roblox Studio keybind, CTRL+L, that allows you to rotate a part on a local axis, instead of global axis. That’s what I mean by “rotation on local axis”.

Sorry if the screenshots are to big, but my computer has a huge screen.

Also this is an example, it should also work with green and blue axis.