I am trying to create a code which will rotate given part in direction of players mouse using alignOrientation. That’s easy but how do i add limits for rotation? Let’s imagine that we need to let part freely rotate on Y axis but make it unable to rotate for more than 45 degrees in both up and down on x axis.
I already tried to do it through geting orientation from :ToOrientation(), clamping by math.clamp() and returning in CFrame.Angles but it only follow limits when facing world front direction. In other case it gives unexpected rotation. And yes I was including difference between radians and degrees.
Here is the part of the code that is responsible for this
--CFrame which will rotate part without including limits
local potentialCFrame = CFrame.lookAt(helicopterRoot.Position, lookPosition.Position)
local pitch, yaw, roll = potentialCFrame:ToOrientation()
local orientationVector = Vector3.new(
math.deg(pitch),
math.deg(yaw),
math.deg(roll)
)
--Clamped orientation which in theory must be ok
alignOrientation.CFrame = CFrame.Angles(
math.rad(math.clamp(orientationVector.X,-45,45)),
math.rad(orientationVector.Y),
math.rad(orientationVector.Z)
)
English isn’t my native language so be aware of misundarstanding. I will be happy to hear any response!