World space CFrame.Angle()

Hi, I’m trying to set the Motor 6D RightShoulder Joint to:

character.RightUpperArm.RightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(MouseProperties.Y,0,0);

I got stuck and I want to know how to set C0 to the world space angle for CFrame.Angles(MouseProperties.Y,0,0). Currently, it’s relative to the UpperTorso.

1 Like

You can do this by using the toObjectSpace and toWorldSpace methods. You can read about the actual operations these methods use here if you’re interested.

So the following example assumes that the CFrame representing the Mouse is world space.

-- this would be world space in this case
local mouseCFrame = CFrame.new(humanoidRootPart.Position, mouseWorldPosition) 

-- put it in localspace with the RightShoulder
local shoulderSpaceMouseCFrame = RightShoulder.Part0.CFrame:toObjectSpace(mouseCFrame)

-- remove any position from the shoulderSpaceMouseCFrame to make it a rotation only CFrame
shoulderSpaceMouseCFrame = shoulderSpaceMouseCFrame - shoulderSpaceMouseCFrame.p

-- apply it to the shoulder
rightShoulder.C0 = CFrame.new(1, 0.5, 0) * shoulderSpaceMouseCFrame

3 Likes

Thanks for helping but when I tried this method, I’m still having issues.
I managed to solve this using Part0(UpperTorso)'s Orientation property.
Then I subtracted the world orientation of UpperTorso from the angle.

local rotation = math.rad(character.UpperTorso.Orientation.X);
character.RightUpperArm.RightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(MouseProperties.Y-rotation, 0, 0);

It works perfectly.

1 Like

TIL CFrame.Angles has an (Vector3 origin, Vector3 lookAt) constructor.

Why isnt this documented in the wiki?

1 Like

Because it doesn’t exist.

print(CFrame.Angles(Vector3.new(5,5,5), Vector3.new(6,6,6)).p)
bad argument #3 to 'Angles' (number expected, got no value)

He just meant to type CFrame.new() and wrote Angles by mistake.

5 Likes

woops :upside_down_face:

1 Like