Dodgy Drivers Part 2
The part of this project that I was most concerned about was the procedural generation. Since this was a small prototype of a game the designers and myself decided that it would be easier to have the map spawn random small sections instead of having premade race tracks.
This code has two important features to it, spawning new pieces of track at an appropriate time, and deleting the old pieces of track when we are done with them so that the game does not slow down too much.
The first thing i needed to do was create a list of GameObjects that would hold all the possible map sections that were designed by the designers, and a list of GameObjects that holds all the currently existing sections in the game ( called currSections). I also needed to have a vector3 to keep track of where the next spawn location should be and a pair of floats to judge how close the current leader was to the next section. These floats were called distanceToNextSection and timeToSpawnDistance.
In the update loop the game would constantly be checking how far the leading player is from the instantiation position. if this distance became less than distance to spawn the function SpawnNextSection() would be called. All this function does is use Random.range to select a random section from the list, and then instantiate this section at the instantiationPosition and add it to a list . after doing that the instantiationPosition would then be set to its previous position plus the length of a section. This is all that was required to handle the spawning of sections.
The other function of the update loop is to check if there were more than 7 map sections currently existing. If there are we call a function called DeleteOldestSection(). This function just access the currSections list and deletes whichever one was at position [0].
The code for all of this can be seen in the photo below.