Feedback on airplane script

I am trying to make a functional aircraft with a throttle system, where the aircraft turns toward where the camera is facing. The aircraft has a maximum turn speed and a current turn speed. The turn speed is determined by the difference in angle between the player’s camera and the aircraft, where facing directly forward will not increment the turn speed at all, and looking 45 degrees to the right will increment it by the maximum possible. (illustrated in picture)

to accomplish this, I’ve drafted this section of code:

local update = function(dt)
	
	--throttle
	if inputs.W == 1 then
		throttle = throttle + (dt * throttleIncrement)
		if throttle > 1 then throttle = 1 end
	elseif inputs.S == 1 then
		throttle = throttle - (dt * throttleIncrement)
		if throttle < 0 then throttle = 0 end
	end
	
	--Speed increment
	local targetSpeed = topSpeed * throttle
	if currentSpeed < targetSpeed then
		--increase speed
		currentSpeed = currentSpeed + (acceleration * dt)
		if currentSpeed > targetSpeed then currentSpeed = targetSpeed end
	elseif currentSpeed > targetSpeed then
		--decrease speed
		currentSpeed = currentSpeed - (acceleration * dt * 2)
		if currentSpeed < targetSpeed then currentSpeed = targetSpeed end
	end
	
	--this ratio is a ratio between the aircraft's current speed and its flight velocity
	local ratio = math.min(currentSpeed/flightVelocity, 1) --flight velocity is the velocity needed for the plane to achieve flight
	
	--apply speed
	plane.PrimaryPart.AssemblyLinearVelocity = plane.PrimaryPart.CFrame.LookVector * currentSpeed
	
	--apply lift and gyro based on the ratio calculated above
	plane.PrimaryPart.BodyForce.Force = Vector3.new(0, game.Workspace.Gravity * planeMass * ratio, 0)
	plane.PrimaryPart.BodyGyro.MaxTorque = Vector3.new(planeMass * .5 * ratio, planeMass * .5 * ratio, planeMass * .5 * ratio)
	plane.PrimaryPart.BodyGyro.CFrame = CFrame.fromOrientation(0, math.rad(90), 0)
	
	
	--determine difference in orientation between the camera and the plane
	local camX, camY, camZ = camCF:ToOrientation()
	local planeX, planeY, planeZ = plane.PrimaryPart.CFrame:ToOrientation()
	
	local turnRatioX, turnRatioY
	turnRatioX = math.min(1, math.max(-1, (camX-planeX)/math.rad(55)) )
	turnRatioY = math.min(1, math.max(-1, (camY-planeY)/math.rad(55)) )
	
	--negates negligible difference in values to allow for straight flight
	if math.abs(turnRatioX) < .06 then turnRatioX = 0 end
	if math.abs(turnRatioY) < .06 then turnRatioY = 0 end
	
	--the target turn is based off of the top turn speed and the ratio of difference in angle
	--looking all the way to the side will yield the greatest turn speed
	local targetTurnX, targetTurnY
	targetTurnX = topTurnSpeed * turnRatioX
	targetTurnY = topTurnSpeed * turnRatioY
	
	local targetTurnSpeed = CFrame.fromOrientation(targetTurnX, targetTurnY, 0)
	--move the turn speed toward the target turn speed
	turnSpeed = turnSpeed:Lerp(targetTurnSpeed, 1 * dt)
	
	--adjust the gyro to rotate the plane by the current turn speed
	plane.PrimaryPart.BodyGyro.CFrame = plane.PrimaryPart.BodyGyro.CFrame * CFrame.new():Lerp(turnSpeed, ratio)
end

While in theory this should work, this is what it looks like in practice:
ae9354869f3e464276c46cb4077086db

6b2caaf81ea4b6f31ec2d36ec5c64611

The movements of the plane are erratic and not in line with the expected behavior

Does anyone know what might be causing this? I can provide other samples of code if necessary

1 Like

Instead of doing some subtraction to get the angle difference. You can do CFrame:Inverse() which will get the difference in a way which will not mess up when going between orientations limit of [-180, 180].

	local camX, camY, camZ = camCF:ToOrientation()
	local planeX, planeY, planeZ = plane.PrimaryPart.CFrame:ToOrientation()
	
local differenceCF = plane.PrimaryPart.CFrame:Inverse() *camCF
local diffX, diffY = differenceCF:ToOrientation()

	local turnRatioX, turnRatioY
	turnRatioX = math.min(1, math.max(-1, (diffX)/math.rad(55)) )
	turnRatioY = math.min(1, math.max(-1, (diffY )/math.rad(55)) )

1 Like

Are you kidding me lol

Ive been writing hundreds of lines of code to manually solve the -180 180 problem on various other vehicles, I’ve been doing it wrong this whole time. Thank you

Edit: weeeelll that said, the anomaly still persists, although this did simplify the code a bunch.