PID controller in script

  1. What do I want to achieve?
    I would like to have a car follow a race line.
  2. What is the issue?
    I have no idea how I would go about making a PID controller in a script.

I looked up solutions but they all had to do with body movers which isn’t what I am looking for.

(I can’t use body movers or tweens also)

Attempt using PathfindingService using the car.

Yes, I am using path finding service but how would I actually get the car to stick on the line with only using the Steer values in vehicle seat?

I mean, body movers like BodyPosition basically are PID controllers. I think there are probably better ways to make a car stay on a line than with a lua PID controller.

Anyways, if you’re set on this, something like this:

  • Your input (PV) would be the distance the car is from the line. I would use something like Ray:Distance for this, but it depends on your setup.
  • Your setpoint is 0 (distance from line)
  • Your output is the steer value (or the rotation force applied to the car? again depends on your setup).

Then just write your ComputePID(input, lastInput, kp, ki, kid, outputLimit) function, bind it to RunService.Stepped, and start tuning your constants.

Thanks for the help! :grinning:

Also just asking how would I get the Output from a BodyPosition?

I’m confused, sorry. Are you using BodyMovers or not? Because if you’re using them, ignore what I said and just update the Position of the BodyPosition every frame. It will handle the force applied for you and everything.

Sorry for the confusion I cant use body movers.