I'm trying to come up with a calculated way to compare current speed vs distance to the next waypoint, with the end goal that the computer car makes a decision to "coast," brake, or keep the throttle on in the best way while turning the best way possible so that it doesn't OVERSHOOT the waypoint (like the Standard Unity assets car currently does when using the waypoint AI). I feel like there's a few things I'm failing to grasp, however. I've had to set up a TON of barriers and something I call "PowerZones" (zones that control the amount of brake and engine torque on the car in terms of percentages). Here's my current course below, which is of course incomplete in waypoints, but the point is to note how many of these zones and obstacles there is. I really don't feel there should HAVE to be this many!
However, the reason that I have SO many is because it's been kinda difficult for me to come up with an ideal function to optimize these kinds of driving and steering decisions as the opponent. Here's the Drive and Steer functions for the vehicle (some of which are based on an existing Eyemaginary tutorial):
private void Drive()
{
float currDistFromWP = Vector3.Distance(transform.position, nodes[currentNode].position);
float speed = rb.velocity.magnitude;
float timeFloatLeft = currDistFromWP / speed;
Vector3 relativeVector = transform.InverseTransformPoint(nodes[currentNode].position);
float newSteer = (relativeVector.x / relativeVector.magnitude);
Debug.Log("Steer to set: " + newSteer);
Debug.Log("Current distance: " + currDistFromWP);
if ((timeFloatLeft < 1f || Mathf.Abs(newSteer) > .5f) && speed > 5)
{
Debug.Log("Coasting...");
im.throttle = 0;
}
else
{
im.throttle = 1;
}
}
private void ApplySteer()
{
if (avoiding)
return;
Vector3 relativeVector = transform.InverseTransformPoint(nodes[currentNode].position);
float newSteer = (relativeVector.x / relativeVector.magnitude);
im.steer = newSteer;
}
Here is my waypoint updating function:
private void CheckWaypointDistance()
{
float currDistFromWP = Vector3.Distance(transform.position, nodes[currentNode].position);
//Debug.Log("Current distance from waypoint: " + currDistFromWP);
if (currDistFromWP < 1.1f)
{
im.brake = false;
if(currentNode == nodes.Count - 1)
{
currentNode = 0;
}
else
{
currentNode++;
}
}
else if (currDistFromWP < 10f)
{
im.brake = true;
}
}
I feel like I could be making this harder than I need to somehow. Or maybe I'm just needing to consider more variables. Does anyone have any tips to make this a little more tight in terms of AI logic? I'm happy to show more code if needed, but the idea is to have a Car Controller class that's used by BOTH the player and AI to DRIVE the car, and then the AI making the best decisions in a way the player would need to drive it in a separate class. The code from above is the separate AI decision making component.