I have a system using PIDs to tilt my drone to cancel out any lateral movement it creates using the upward thrust I have on the drone and redirecting some of it in a given direction. The problem arises when my drone tilts to cancel the lateral movement out and it does successfully but it doesn’t take into account that it is still tilted and moves itself the other direction. It then tries to correct the lateral movement in the other way that it created itself and rinse and repeat till it crashes. I am wondering how I would go about making the tilt 0 as the lateral velocity also reaches zero. I am using torque to control the tilt of the drone. The code I use to achieve this is shown below
Video: https://gyazo.com/2b36021b0150be85fd9ffe6f6a69a609
local PID = require(RS.Modules.PID)
-- From https://github.com/Sleitnick/AeroGameFramework/blob/master/src/StarterPlayer/StarterPlayerScripts/Aero/Modules/PID.lua
local XRotTorque = PID.new(-1,1,100,200,0)
function Update(dt)
local ObjectRot = script.Parent.Engine.CFrame:VectorToObjectSpace(script.Parent.Engine.Orientation)
local ObjectVelocity = script.Parent.Engine.CFrame:VectorToObjectSpace(script.Parent.Engine.AssemblyLinearVelocity)
local XRotValue = XRotTorque:Calculate(dt,0,ObjectVelocity.Z)
script.Parent.Engine.Torque.Torque = Vector3.new(XRotValue/10,YRotValue * 3,0)
end
RunService.Heartbeat:Connect(Update)