Plane rolls when pressing A/D but after a while starts going the other direction, then the correct direction again

Hey all,

I have a bizarre glitch as follows;

When the user press A/D to roll, the plane begins to do exactly what it’s supposed to. However, after about 1.5 complete rotations the plane slows down to a halt and begins to roll the other direction, stop again, then start rolling the correct direction once again, completing another 1.5 rotations, and the cycle continues. The other direction has the same result.

I am not sure why this is happening, here is the portion of the script that handles rolling:

runservice.RenderStepped:connect(function()
	if accelerate == true then
		speed = speed + acc
	end
	if decelerate == true then
		speed = speed - acc
	end
	if speed < 0 then
		speed = 0
	end
	if speed > maxspeed then
		speed = maxspeed
	end
	if right == true then
		tilt = tilt - 5		
	end
	if left == true then
		tilt = tilt + 5		
	end
	if speed < minspeed then
		speed = minspeed
	end
	buttons.Line.Indicator:TweenPosition(UDim2.new(convert(speed,maxspeed,1),-5,0,-5),"In","Linear",0.1,true)
	if active == true then
		if click == true then
			pos = mouse.Hit.p
			local target = engine.Position+((pos - engine.Position).unit*100)
			dir = (target - engine.Position).unit
		end
		if dir ~= nil then
			wait()
			engine.BodyGyro.CFrame = CFrame.new(pos,  pos + dir) * CFrame.Angles(0,0,math.rad(tilt))
		end
		engine.BodyVelocity.velocity = engine.CFrame.lookVector * speed
	end
	local adb = false
	if active then
		if not adb then
			adb = true
			fuelsender()
			adb = false
		end
	end
end)

In most planekits the roll thing is two different bits of code (i.e. if rolling this way we do this, if they roll that way do that) but in my code either directions are controlled by the same one bit of code. This may have implications somehow? I have no idea…
Does anyone know what is going on?

2 Likes

Have you tried printing out the values of variables that control the movement?

I noticed there is some seemingly unnecessary math here. Why are you calculating a target then a direction then adding that direction to the position in the end? Couldn’t you just eliminate that and replace

pos + dir

with

mouse.Hit.Position

Also, please post the part of the code that calculates the tilt value.

runservice.RenderStepped:connect(function()
	if accelerate == true then
		speed = speed + acc
	end
	if decelerate == true then
		speed = speed - acc
	end
	if speed < 0 then
		speed = 0
	end
	if speed > maxspeed then
		speed = maxspeed
	end
	if right == true then
		tilt = tilt - 5		
	end
	if left == true then
		tilt = tilt + 5		
	end
	if speed < minspeed then
		speed = minspeed
	end
	buttons.Line.Indicator:TweenPosition(UDim2.new(convert(speed,maxspeed,1),-5,0,-5),"In","Linear",0.1,true)
	if active == true then
		if click == true then
			pos = mouse.Hit.p
			local target = engine.Position+((pos - engine.Position).unit*100)
			dir = (target - engine.Position).unit
		end
		if dir ~= nil then
			wait()
			engine.BodyGyro.CFrame = CFrame.new(pos,  pos + dir) * CFrame.Angles(0,0,math.rad(tilt))
		end
		engine.BodyVelocity.velocity = engine.CFrame.lookVector * speed
	end
	local adb = false
	if active then
		if not adb then
			adb = true
			fuelsender()
			adb = false
		end
	end
end)

So I have a suspicion that the BodyGyro cannot keep up with your rotation value, as you are adding 5 degrees 60 times per second, which means it should rotate 360 degrees in 1.2 seconds. Obviously it isn’t rotating that fast. Because the BodyGyro is trying to rotate to the desired angle by the shortest path, sometimes it will reverse. You can fix this by lowering the ‘tilt = ± 5’ to a lower number, or by changing the D, P, and MaxTorque values in the BodyGyro. I would recommend just lowering the 5 to maybe 2.

3 Likes

it works perfectly now. Thank you so much!

1 Like