I’ve been trying for the past 2 days to figure out a way to make this PID controller stabilize a hovering vehicle using a vectorForce. I just simply cant figure out the correct PID gains, its either oscillating intensely or not moving at all. But no matter what gains I put, it NEVER stabilizes at the height I’m trying to get it to hover at.
–Notes:
- Everything is done in a local script while using RenderStepped.
- I have workspace gravity set to 0, as I am applying gravity directly in the script on the vehicle.
- I am using @BRicey763 RoPID Module: RoPID: PID Control in Roblox!
- The vehicle is only one part with its “Massless” property turned on
Here is the script responsible for calculating all the forces.
–Variables:
- GlobalVehicleSettings.HoverHeight = 15
- GlobalVehicleSettings.RayLength = 50
- GlobalVehicleSettings.HoverForce = 300
- GlobalVehicleSettings.HoverGravity = 200
- GlobalVehicleSettings.FallGravity = 800
--Function to calculate the vehicles Hover
local function CalculateHover(dt)
--Shoot a ray to check if the vehicle is on the ground
local HoverRay = workspace:Raycast(MainVehicleInfo.CenterVectorAtt.WorldPosition,(MainVehicleInfo.CenterVectorAtt.WorldCFrame.UpVector*-1)*GlobalVehicleSettings.RayLength)
--Local Vars
local GroundNormal
--If the raycast hits and the ship is on the ground
if HoverRay then
--Save the rayinfo in local vars for better readability
local Height = HoverRay.Distance
GroundNormal = HoverRay.Normal
--Calculate the hover force using Briceys RoPID module
local ForcePercent = MainVehicleInfo.PIDController:Calculate(GlobalVehicleSettings.HoverHeight,Height,dt)
Base.Position+MainVehicle.VehicleBase.CFrame.UpVector*ForcePercent)
--print(ForcePercent)
--Calulate the total hover force vector required based on the normal of the raycast
local Force = GroundNormal * ForcePercent * GlobalVehicleSettings.HoverForce
--Calculate the gravity relative to the ships CFrame
local Gravity = -GroundNormal * GlobalVehicleSettings.HoverGravity * Height
--Apply the force to the VectorForce of the vehicle
MainVehicleInfo.CenterVectorAtt.VectorForce.Force += Force
MainVehicleInfo.CenterVectorAtt.VectorForce.Force += Gravity
else --If the vehicle is not on the ground
--The normal used is the global up vector
GroundNormal = Vector3.new(0,1,0)
--Calculating and applying gravity
local Gravity = -GroundNormal*GlobalVehicleSettings.FallGravity
MainVehicleInfo.CenterVectorAtt.VectorForce.Force += Gravity
end
end
Here is what the current PID gains are and what it looks like
(“LineA” and the moving red line on the part are just representations of the ForcePercent Value from the script above)
Any help on how to stabilize the vectorForce is greatly appreciated, Thanks!