# Steering an AI vehicle to a waypoint using float of -1 to 1?

NEW (Updated) QUESTION:

I created a simple AI to follow waypoints, by getting the angle of my vehicle, and the angle to the next waypoint, and having my vehicle turn to face that angle. Its not the best though - I need a better way to get it from waypoint to waypoint while maintaining its lane!

Now it finds its next waypoint, and tries to drive around, but, it sort of starts “oscillating” around the line to the next waypoint (my lanes). I got rid of it a little, by expanding my float from just -1 or 1, to allow numbers in between, but now it sort of is slower to respond, but drives wit less oscillation now. How can I get it really accurate to its lane, so it looks natural in its movement and maintains that lane better?

this bit of code deals with getting to the actual waypoints by steering:

``````FindAngle();

// Right Turning
if(DesiredAngle - OurAngle >= 3 && DesiredAngle - OurAngle <= 18)
{
CarControlAI.SteerInput = 0.2f;
}
if(DesiredAngle - OurAngle >= 19 && DesiredAngle - OurAngle <= 36)
{
CarControlAI.SteerInput = 0.4f;
}
if(DesiredAngle - OurAngle >= 37 && DesiredAngle - OurAngle <= 54)
{
CarControlAI.SteerInput = 0.6f;
}
if(DesiredAngle - OurAngle >= 55 && DesiredAngle - OurAngle <= 72)
{
CarControlAI.SteerInput = 0.8f;
}
if(DesiredAngle - OurAngle >= 73 && DesiredAngle - OurAngle <= 90)
{
CarControlAI.SteerInput = 1.0f;
}

// Left Turning
if(DesiredAngle - OurAngle <= -3 && DesiredAngle - OurAngle >= -18)
{
CarControlAI.SteerInput = -0.2f;
}
if(DesiredAngle - OurAngle <= -19 && DesiredAngle - OurAngle >= -36)
{
CarControlAI.SteerInput = -0.4f;
}
if(DesiredAngle - OurAngle <= -37 && DesiredAngle - OurAngle >= -54)
{
CarControlAI.SteerInput = -0.6f;
}
if(DesiredAngle - OurAngle <= -55 && DesiredAngle - OurAngle >= -72)
{
CarControlAI.SteerInput = -0.8f;
}
if(DesiredAngle - OurAngle <= -73 && DesiredAngle - OurAngle >= -90)
{
CarControlAI.SteerInput = -1.0f;
}

if(DesiredAngle - OurAngle < 3f && DesiredAngle - OurAngle > -3f)
{
CarControlAI.SteerInput = 0;
}
``````

I guess I need to get a smoother transition between floats, as well as knowing to turn sharply if the waypoint is close…hmm

ORIGINAL QUESTION (Not dealing with this same problem so much):

I have a physics based vehicle AI, specifically the dodge charger from the unity car tutorial “alt” system. It takes input to move forward and brake, and a float for turning between -1 and 1.

I need to take my current waypoint and get the angle of that waypoint from the car, and start turning the vehicles wheels to face the waypoint. I feel dumb but all my attempts are junk so far.

How can I move them around by waypoints: Clue me in, how do you point your car at those waypoints with -1 to 1 input? Do you take the angle in relation to your car, then just turn until that angle gets smaller?

How could this be done? You rock if you have an actual script showing how to do this, and you really rock if its in C#

So let me clarify, the steering of my PHYSICS BASED VEHICLE is controlled by a float, which is from -1 to 1, and if its -1, steering is completely left, and if its 0.5 then steering is halfway to the right yaknow…

Here is a quick image to show you what I want incase I am explaining bad:

I’m not EXACTLY sure what you are asking for. Do you want the car to turn fully to the left if the angle between the waypoint and the car is negative? and vice versa if positive? This code should rotate the car towards the waypoint (not tested so not sure).

``````public Vector3 carPos;
public Vector3 waypointPos;

Vector3.RotateTowards(carPos, waypointPos, speed * Time.deltaTime, 0f);
``````

Or maybe you can check the angle between the two objects and if it is negative return -1 otherwise return 1.

``````	public int returnDirection(Quaternion carRot, Quaternion waypointRot) {

float angle = Quaternion.Angle(carRot, waypointRot);

if(angle > 0 && angle < 90) {

return 1;

} else if(angle > 90 && angle < 270) {

return -1;

} else if(angle > 270 && angle < 360) {

return 1;

} else {

return 0;

}

}
``````

Okay so here is what I’m thinking:

``````public float returnDirection(carRotation, waypointRotation) {

float angle = Quaternion.Angle of carRotation and waypointRotation

return the sin of "angle"

}
``````