How Do I Completely Remove the Z rotation aspect of a CFrame?

I’m trying to make a rotatable cannon but sometimes the cannon starts to go on its side like so:

image

Everything turns right/left/up/down on queue but it is still possible for you to lay on your side like that. Therefore, I tried to remove the Z rotation part by setting it to 0 no matter what.

– adjust the cframe by the y-angle and the x-angle.
Body.CFrame = Body.CFrame * CFrame.Angles(0, math.rad(yAngle), 0) * CFrame.Angles(math.rad(xAngle), 0, 0)

local x,y,z = Body.CFrame:ToEulerAnglesXYZ()

– if the body tilted to the side, then no worry. reset that tilt back to zero and preserve the other old angles.
Body.CFrame = CFrame.new(Body.Position) * CFrame.Angles(x, y, 0)

Clearly I lack an understanding of CFrames because instead my cannon seems to flick back and forth in arbitrary rotations. (Also where is that script formatting option so I can format my code?)

Rather than asking how to strip the Z rotation (Which can be case specific) how about you share where the angle is being set and all of the inputs? It would be a lot easier to help you.

1 Like

My (probably inefficient) version of doing this is just multiplying them individually.

Edit: CFrame.new(pos.X*i, pos.Y*i ,pos.Z*i)
If you don’t want it to move you can just replace ‘i’ with 1

Also, you can make it point towards an object or Vector3 value, Roblox covers it here: CFrames | Documentation - Roblox Creator Hub

Edit 2: If this works please mark it as Solution