Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get ONE rotation value from a CFrame?

After having learned how to put one position with another rotation, I now need to figure out how to get one rotation value (rY). I'm trying to rotate my character depending on the direction of the camera, except I only want the character to be able to spin along the y axis, not the x or z, or else he won't be standing up straight. So, how would I simply get the rotation value of the camera on the Y axis while leaving the Z and X at 0?

1 answer

✓ Accepted answer

The rotational part of CFrames can be converted to Euler angles. This is basically three numbers representing the rotation of each of the three axes. We can get the Euler angles of a CFrame called cf by doing local x , y , z = cf:ToEulerAngles(). We can then construct a rotational CFrame again using CFrame.Angles(0 , y , 0). The zeroes in the x and z fields mean that the x and z angles of the character won't be changed.

local camera = workspace.CurrentCamera
local x , y , z = camera.CFrame:ToEulerAngles()

local rotation = CFrame.Angles(0 , y , 0)
local position = CFrame.new(humanoidRootPart.Position)

local result = position * rotation

character:SetPrimaryPartCFrame(result)

3 comments

0

Thanks again! I've learned so much from you today lol

1

For the record. As of 2019 the method is now :ToEulerAnglesXYZ()

0

ERROR: {ToEulerAngles is not a valid member of CFrame} ?!

This discussion is preserved as it was. New answers, comments, and votes are closed.