Steer towards target

So, this is somewhat of a loaded question and I will try to do my best to explain it.

I am creating a vehicle AI system, and the vehicle has a Steer(x) function. X ranges from -1(steer left) to 1(steer right).

I’m trying to find the value that I need to pass to that function to steer my vehicle accordingly to a target, in this case the next checkpoint.

An example: Imagine a vehicle to the right of a checkpoint, this function should return something like -0.2 to steer the vehicle back towards the checkpoint.

I’ve posted this question on other forums, and had some pretty indirect answers that more or less just confused me. All of them did bring up trig functions however, but it’s somewhat difficult for me to understand being a freshmen in HS.

If somebody could provide any insight on the way to achieve something like this, it would be awesome. Thanks!

I actually accomplished something similar by doing some vector manipulations.
if a is the vector from the vehicle to the target and b is the target’s forward direction, you can get a -1 to 1 steering value by doing Vector3.Dot (Vector3.Cross (a.normalized, b.normalized), Vector3.Up);

Vector3.Cross (a.normalized, b.normalized) gets you a vector pointing either directly up or directly down (assuming you’re on flat terrain) with lengths from 0 to 1.
The dot product with Vector3.Up will then give you a range from -1 to 1 with -1 being “Target is 90 degrees to the left” and 1 being “Target is 90 degrees to the right”. Smaller steering values are given if you’re more-or-less on the right track.

I don’t have the code with me anymore, so I cant remember for sure if we used Vector3.Up or Vector3.Down, but it should be pretty easy to figure out which to use with trial and error.

Disclaimer, this ONLY works if the target is actually in front of the vehicle. If it’s behind (even by just a little bit) you’ll get the wrong steering. We solved this by having lots of checkpoints around curves, and generally not having and sharp (>90 degree) angles between checkpoints.

I have the exact same problem. I followed the unity wheel collider tutorial, but changed the steering part. So far my car just goes in circles. My code looks like this:

directionAngle += Input.GetAxis("Horizontal") * 0.01f;
    rotationAngle = transform.rotation.y;
    if (rotationAngle > Mathf.Sin(directionAngle))
    {
      steering = rotationAngle - Mathf.Sin(directionAngle);
    }
    else
    {
      steering = rotationAngle + Mathf.Sin(directionAngle);
    }
    float clampedSteering = Mathf.Clamp(steering, steeringDampening, -steeringDampening);