How Can I lock the X & Y Rotation Of a Part With CFrames?

I’m making a sort-of VR Shooter game, But then I was thinking of how to make the inventory system for Magazines. And I thought of a concept of where you had 4 Circles That would track the rotation of your or “Simulate” the rotation of your torso.

But I got stuck on how I could lock the rotation of the torso on the X & Y axis
so that only the Y axis could rotate.

If you want an example then, this is what i’m trying to achieve.

And, this is what i’m not trying to achieve.

you can use the CFrame.Angles function to create a new CFrame with the desired rotation.

how you can lock the X and Y rotation of a part

-- Create a new part and set its CFrame to the player's torso
local part = Instance.new("Part")
part.CFrame = player.Torso.CFrame

-- Lock the X and Y rotation of the part
local xRotation = 0 -- Set the X rotation to 0 degrees
local yRotation = player.Torso.Rotation.Y -- Set the Y rotation to match the player's torso
local zRotation = player.Torso.Rotation.Z -- Set the Z rotation to match the player's torso
part.CFrame = CFrame.Angles(math.rad(xRotation), math.rad(yRotation), math.rad(zRotation))

-- Parent the part to the player's character model
part.Parent = player.Character

I’ve Sort-of found a way to replicated of what i’m trying to do, but now I have a new issue.

The torso is now “Swinging” instead of staying directly below the Users Head CFrame
robloxapp-20230707-1936371.wmv (3.1 MB)

1 Like

for locking the rotation of the torso on the X and Y axis, you can use the EulerAngles property of the CFrame to create a new CFrame with the same position as the torso, but with the X and Y rotations set to zero.

-- Create a new part and set its CFrame to the player's torso
local part = Instance.new("Part")
part.CFrame = player.Torso.CFrame

-- Lock the X and Y rotation of the part
local xRotation = 0 -- Set the X rotation to 0 degrees
local yRotation = player.Torso.Rotation.Y -- Set the Y rotation to match the player's torso
local zRotation = player.Torso.Rotation.Z -- Set the Z rotation to match the player's torso
part.CFrame = CFrame.new(player.Torso.Position) * CFrame.Angles(0, math.rad(yRotation), 0)

-- Parent the part to the player's character model
part.Parent = player.Character

Make sure that youre updating the CFrame in a way that takes into account the player’s head CFrame and updates the part’s CFrame accordingly.