Need help locking a model's PivotTo() to a single axis

i have a model that is composed of 2 segments, one should only be able to rotate horizontally (left and right), the other should only be able to rotate vertically (up and down), like the turret on a tank.
the current system uses PivotTo() and me and a few other people are stumped on how to get it to function as i desire. help would be nice.

You can construct CFrames that are based around an axis with CFrame.fromAxisAngle(axis, r):

local yaw = 0 --Rotation "left and right" (in degrees)
local pitch = 0 --Rotation "up and down" (in degrees)

--This CFrame will rotate "left and right"
local yawCFrame = CFrame.fromAxisAngle(Vector3.yAxis, math.rad(yaw))
--This CFrame will rotate "up and down"
local pitchCFrame = CFrame.fromAxisAngle(Vector3.xAxis, math.rad(pitch))

--Assuming there is some origin position for the turret base
local turretCFrame = CFrame.new() --Change to your turret base
[PartThatRotates]:PivotTo(turretCFrame * yawCFrame)
[PartThatRotatesAndTilts]:PivotTo(turretCFrane * yawCFrame * pitchCFrame)

If it the rotation is in the wrong direction, add a negative sign in front of either of the arguments to the CFrame.fromAxisAngle calls.

1 Like

will try this whenever, thank you

wait, does yaw and pitch have to be set to a specific value?

No, the yaw and pitch variables are the inputs that affect the rotation of the turret arm. For both, 0 represents no rotation (straight forward).

so if i set pitch to 45 it would be looking 45 degrees upwards automatically?

Assuming you fill in the other parts of the code that I have no way of knowing (the two lines that call the PivotTo methods).