How to restrict CFrame along X & Z Axis?

I’m working on a flight system that I ideally wish to have restrictions:-
I.e. When the Player is turning Left / Right, I want the ship to tilt on the X axis by a maximum of 45 Degrees on either side and when the Player is turning upwards or downwards, I want the ship to turn a maximum of 85 Degrees either side.

Currently I use the code shown below to determine the CFrame required however, it’s proven to be extremely.

			local rot = CFrame.fromEulerAnglesXYZ(0,0,0)
			BodyGyro.CFrame = (rot*(BodyGyro.CFrame-BodyGyro.CFrame.p)) + BodyGyro.CFrame.p

			local x,y,z = BodyGyro.CFrame:ToOrientation()
			BodyGyro.CFrame = CFrame.fromOrientation(math.rad(tiltAngleDiv), math.rad(TurnAngle), math.rad(turnAngleDiv))

			if PilotSeat.Throttle == 1 then
				if TiltIncline < 85 then
					TiltIncline += 2
				end
			elseif PilotSeat.Throttle == -1 then
				if TiltIncline > -85 then
					TiltIncline -= 2
				end
			end

			if PilotSeat.Steer == 1 then
				TurnAngle -= 2
				if Incline > -24 then
					Incline -= 2
				end
				
			elseif PilotSeat.Steer == -1 then
				TurnAngle += 2
				if Incline < 24 then
					Incline += 2
				end
				
			elseif PilotSeat.Steer == 0 then
				if Incline > 0 then
					Incline -= 2
				elseif Incline < 0 then
					Incline += 0.5
				end
			end

			turnAngleDiv = Incline / turnInclineAngle
			tiltAngleDiv = TiltIncline / tiltInclineAngle

When the Player enters Hyperspace - This code is ran the keep the variables the same when entering Hyperspace in whichever direction. However, it results in extreme shaking - Is there a more efficient way I can do this?

TiltIncline = Main.PrimaryPart.Orientation.X
tiltAngleDiv = TiltIncline / tiltInclineAngle
				
Incline = Main.PrimaryPart.Orientation.Z
turnAngleDiv = Incline / turnInclineAngle
				
TurnAngle = Main.PrimaryPart.Orientation.Y

Shaking is vague but noticeable if you look at the edge(s) of the Baseplate.

EDIT
So far, I can limit the jittering movement by rounding the X & Z axis to the nearest (Even) Number; however, this is proving to be impractical also as when the Player leaves Hyperspace - The ship realligns to match the new axis - I’m looking to keep it smooth.

Not sure which angles you mean, make sure to proper the proper right hand rule for talking about rotations along an axis

Do you mean rolling in order to produce a plane turning motion? If so according to roblox Axises, this rolling motion is rotating around the Z axis of the orientation.

image

As seen with an attachment in a dummys head at (0,0,0) base orientation.

Rotating around the X axis leads to pitch axis, or “look up” “look down” movement.

Rotating around the Z axis leads to roll axis movement, or “Head tilt left”, “head tilt right” motion.

Perhaps you modeled the PrimaryPart of your spaceship with a rotated base?

Anyhow, for restricting a CFrame my main method is to convert it to orientation and clamp it then reconvert it to orientation again like you have done.

Summary
--Using CFrame.lookAt method with mouse
	local goalCFrame = CFrame.lookAt(vehicleSeat.CFrame.Position,mouseHit)
	
	local x, y, z = goalCFrame:ToOrientation()
	local newX = math.clamp(math.deg(x),-10, 10)
	newX = math.rad(newX)
	
	local newGoalCFrame = CFrame.fromOrientation(newX,y,z)

However if you are doing a modifying numbers technique Instead of if statements I would just add it then clamp it.

--Do the +- accorrding to key bind
TiltIncline += 2
TiltIncline -= 2
--Then clamp it, saves a lot of space vs if statements
TiltIncline = math.clamp (TiltIncline , -85, 85) 

For the shaking problem, not sure. It seems like you got 2 BodyGyro CFrames?

1 Like