Racing Game Track Generation

Right now, I am developing racing game in 2D that is similar to Road Fighter.

For this I want to create fixed level game play. I have some confusion in mind for track generation.

I have two consideration in mind but which one is best for me that I can’t able to decide.

  1. Load whole racing track in start of game
  2. Load one segment of track at a time and generate next one based on car movement

Implementing first approach is simple one but if I choose second one then how to store data about each segment and car, power up and obstacle information.

I want suggestion in this matter what approach I have to use for game so that it run smoothly on lower ended devices also.

I would recommend to go with 2nd choice (i myself use that technique). Use Object pooling mechanism to generate road.

Since Low end devices have RAM issues so loading whole track at once wouldn’t be good idea. And one more thing Unity 4.3 support ARM 7 devices (doesn’t include Ldpi).

if you have pro, you have Occlusion culling which means what is out of view is not render.

If you do not have it, then you can cut your track in mini parts, load them into an array and deactivate them all except the first and second ones.

Then you place trigger boxes (or check for distance) and when entering the box you deactivate the one behind and activate the one in front. In the end you always have only two parts active at a time.

Those portions of track could also include the AI cars, the ones just driving but not racing.

EDIT: Since your camera is not moving, let’s see another approach.

Each portion of track is under an empty gameObject and all those empty GO are under a main empty GO. This contains the track and a script to generate cars.

All cars are generated on start and placed in a pool of objects so that you do not have to instantiate and destroy them, you simply activate/deactivate. This happens in the start.

Also in the start, all portion object are placed in a dictionary. For instance if your portions are 20 units long then it means the first is at 0, second at 20 and so on. The position is the key and the object is the value.

public Transform mainTrackObject;
Dictionary<int, GameObject> dict = new Dictionary<int, GameObject>();
void Start()
{
   foreach(Transform t in mainTrackObject){
      int key = Mathf.RoundToInt(t.position.z);
      dict.Add(key,t.gameObject);
      t.gameObject.SetActive(false);
   }
   dict[0].SetActive(true);
}

Thanks to this you can “track” how much distance the car have done (even though it is not moving) and activate the portion.

float distance;
void Update(){
   distance += Time.deltaTime * velocity; // Velocity is the assumed speed of the car
   int dist = Mathf.RoundToInt(distance);
   if(dist%20 == 0){
       SetActivation.SetActive(dist);
   }
} 
void SetActivation(int key){
   GameObject o = dict[key];
   o.SetActive(true);
   dict[dist - 20].SetActive(false)
   // Get AI component for cars and call init method
   o.GetComponent<CarAI>().Init();
}

If speed is constant, it is even more simple, you can simply do the activation/deactivation based on time using InvokeRepeating.

Concerning CarAI is could be a simple script with how many car and then place them on the road:

public int amountCar;

void Init(){
   for(int i = 0; i < amountCar;i++){
      GameObject car = PoolSystem.Pop();
      car.SetActive(true);
      // Position system, either random
      // using SphereCast to detect if an object is already there
      // or using an stack of predefined position on track
   }
}

Concerning the stack pooling system I leave to you as you can find this on the internet.

THIS IS JUST GIVING YOU IDEAS, TAKING THE CODE AS IS MAY NOT WORK RIGHT AWAY, YOU MAY NEED TO TWEAK A LITTLE.