How to change rotation of a CFrame?!

I want to keep the X and Y axis of a CFrames rotation and set Z to 0. How do I do this?

I found this post but I don’t understand it

4 Likes

Always check developer.roblox.com for anything Roblox:
CFrames | Roblox Creator Documentation

2 Likes

If the developer hub answered my question, I wouldn’t be here.

Please only post if you have an answer

42 Likes

You could create a new Vector3 by getting the X and Y of the CFrame, something like

local RotationXY = Vector3.new(CFrameValue.Rotation.X, CFrameValue.Rotation.Y, 0)

heres some code, but its untested

part.CFrame = CFrame.Angles(RotationX, RotationY, RotationZ)

If you want to use degrees instead of radians:

part.CFrame = CFrame.Angles(math.rad(RotationX), math.rad(RotationY), math.rad(RotationZ))

If you just want to add the angle to the CFrame, multiply the CFrame and the CFrame.Angles

1 Like

To get the rotation convert the CFrame into orientation. I’m assuming you are talking about orientation since you never specified the rotation type and not eulerangles XYZ.

Local x,y,z = someCFrame:ToOrientation ( )

Then reconstruct the CFrame using

CFrame.fromOrientation(x,y,z)

Or

CFrame.Angles

Depending on the rotation order in which the angles are applied you used previously.

Remember units are in radians. And also to add back the position component if someCFrame had one:

Local x,y,z = someCFrame:ToOrientation ( )
local newCFrame = CFrame.fromOrientation(x,y,0)+someCFrame.Position
17 Likes

Okay I realized the problem I am having.

I didn’t know anything about a rotation matrix or what a euler is, but I realized the rotation comes out completely differently if you do ZYX or YXZ (hence the cframe functions). I solved my problem with new information I found out.

Anyway I’ll mark your post as solution since it answers the question in the original post.

Thanks for your help!