- Home /
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.
Load whole racing track in start of game
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.
Answer by fafase · Apr 25, 2014 at 05:49 AM
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.
I have unity pro. For my game, I don't move my camera but I am moving background. So occlusion culling, will give any benefit to me? Also setting this also found difficult for me.
For second approach, I have some confusion in $$anonymous$$d.I have to load all tracks in game and deactivate rest ones those are not used or put each track based on necessity.
All other data I understand what you have said. Last one, How to manage other data for each track such as cars, power ups etc?
Thanks this is more than enough for me. One last question about car plotting on track. How to place car at random position when I have variety of tracks available?
It would depend on how the portion looks. you could define an array of rect, first pick a rect randomly then pick a position randomly within the dimensions.
If your portion is straight, then only one rect is fine, if your portion is curving then you may need a few of them.
You can also place points on your track and use http://docs.unity3d.com/Documentation/ScriptReference/Random-insideUnitCircle.html
to generate a random position around that point.
$$anonymous$$ake sure to check that a car is not already there with https://docs.unity3d.com/Documentation/ScriptReference/Physics.CheckSphere.html
Answer by Noob_Vulcan · Apr 24, 2014 at 07:16 AM
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).
Thanks for your response. In second approach how to load tracks and other game object? $$anonymous$$eans what coding standard I have to use?
I didn't get what you mean by coding standard... Create few track prefabs ie. say 10 then use these tracks only. You have to reposition these track prefabs again and again. I hope you got what i mean. :)
Say I have 10 tracks, For each track I want fixed data. For ex, in track 3 I want 3 red cars and 2 power ups. Then how to manage this type of data through coding?
you will have to use empty gameobjects say red_car_spawner , fuel_spawner. These gameobjects will be child of the track3.Now you depending on player car position you have to spawn other cars and powerups.
Thanks for your attention. I am talking about data storage of all these information.
Your answer
Follow this Question
Related Questions
In sequence position game objects 1 Answer
Game Development Approach 0 Answers
GameObjects creation within boundry 3 Answers