After I developed and successfully created the waypoint system for the AI, it was time to make the car successfully perform a lap around a track! Firstly I needed the track to start making the new nodes/marks for. My team mate Kyle Williams moddelled and produced the tracks. Here is the track for race 1
to create the AI on this track I first had to create the waypoints/nodes. I did this by creating a parent object called "Track Waypoints" and then all the children being the nodes themselves which were called "Mark". They would then be called Mark1, Mark2, Mark3 etc. This would carry on for the entire track until it looped back to Mark1, Here is what it looked like.
The white line shown in these screenshots represents the exact route the AI car will drive on during gameplay, I had to place all marks/nodes individually and rename them, this became an issue as it would take an extended period of time to individually rename them so I made a script to save time.
I had to rename the Marks/nodes to Mark1, mark2, mark3 etc otherwise the code that allows the car to see the nodes won't work. This is because the car needs the numbers on the end to know exactly where to go. Since renaming them one at a time is very time consuming, I made a script that allows me to rename them all very quickly, here is the script and how it works.
After I finished the Name Changer system I ran into yet another problem, the AI cannot slow down. Therefore it can't complete turns since it goes too fast. So the next step would be to code certain points that let the car know that it needs to slow down. I first came up with the idea of a box trigger called Slow Zone that would make the car slow down when necessary. This box would be completely adjustable so I can adjust it if it doesn't work well.
The code here shows that when the AI car collides with an object with the tag "SlowZone" it will make the boolean "IsInZone" true which when it is true, enables the SlowDown function, which will stop the car from accelerating and it will apply the brakes.
All in all the slow zone system did work but it seemed a little scuffed and impractical, so I came up with a new system that would be more precise than the slow zone system. I discovered that I could code certain things to happen when the AI car reaches a certain node, I used a variable called Current Node which tells me where the AI car is on the track. I developed this code to help the car slow down at certain points.
This code works by saying if CurrentNode is greater or equal to a certain node, it will either slow down, accelerate or change how the steering works.
I later refined these if statements to work a little better and I added 2 new functions: Cornering and Accelerating. The cornering function contains all the necessary values to slow the AI car down such as brake force and drag. The accelerating function allows the car to accelerate. Here is the new refined code I made.
I also noticed I can track the cars speed so I can keep the car at a constant speed going around a corner, to allow the car to not be too slow so the player can pass easily but also not too fast that it can't make the turn.
Here is a video that I uploaded to YouTube where I present my AI car after I completed the Race 1 code.