- Home /
Steering an AI vehicle to a waypoint using float of -1 to 1?
NEW (Updated) QUESTION:
Forum Thread: http://forum.unity3d.com/threads/161147-Basic-City-Traffic-System-Need-help-getting-it-working-right.
WebPlayer: http://ninjeticstudios.com/WebPlayerAIDriver/Webplayer/webplayer.html
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:
you can use a script to deter$$anonymous$$e whether the next waypoint is on the right or left, and then just turn right/ left directly
Answer by kag359six · Dec 02, 2012 at 09:39 PM
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"
}
lol sorry i forgot to set the return type ill update the answer
I changed the code for returning -1 and 1. before it didnt work. But now if you are facing in the direction of the waypoint, it should return positive, and will return negative if facing away. The catch though is that it only returns -1 or 1, not in between. It's not exactly what you want but it's a start.
I was playing around with that code, and I think if you return the Sin of the angle between the car and waypoint it will get you what you are looking for. That way it will return a value for every direction, such as 1 if it is 90 degrees, or 0.7 if it is 45 degrees. However when i used Unity's mathf.Sin it doesn't return the same value a calculator would, so you might need to find a work around. It may have something to do with Unity using radians but I'm not familiar with that. I'll write pseudo code for you (its not usable) to demonstrate what i mean. (will be in an edit of my answer)
new problem same subject - see revised question
$$anonymous$$aybe you should set waypoints for each lane, and depending on the lane the car is in, you use the waypoints for, lets say, the left lane, or the right lane depending on where the car is. I'm not sure as it is getting difficult to keep up with your progress lol. Perhaps setting a variable (such as the -1 and 1 that you used for turning) for what lane the car is in can help. $$anonymous$$y thoughts are kind of all over the place, so let me sum it up. $$anonymous$$ake two seperate sets of waypoints for each lane, and depending on what lane you want the car to be in, you change a variable that switches the lane and therefore changes the set of waypoints.
Your answer
Follow this Question
Related Questions
Unity 5 Standard Asset Car Waypoint AI problem with accuracy 0 Answers
Steering Overcompesation 0 Answers
how to make a vehicle drive down a road 1 Answer
Cars in City 0 Answers
Check whether car has passed a point in world space 1 Answer