Ad
Log in to vote
1

How to get ONE rotation value from a CFrame?

Asked by 3 years ago

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

Log in to vote
1
Answered by
RayCurse 1513 Moderation Voter
3 years ago
Edited 3 years ago

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)
0
Thanks again! I've learned so much from you today lol cowsoncows 926 — 3y
1
For the record. As of 2019 the method is now `:ToEulerAnglesXYZ()` joeldes 201 — 1y
0
ERROR: {ToEulerAngles is not a valid member of CFrame} ?! Newh0hrizonCz 58 — 3mo
Ad

Answer this question