- Home /
How would I efficiently implement race positions?
I am trying to put racing positions i n my game. I decided to have a race manager script with an array of all racers, and then it sorts the array of racers so they can have the correct position depending on their index in this array. I decided to use bubble sort because I only have 3 racers and it is simple. First I check compare their lap counts, if same, then I compare their current checkpoint hey are on, if same, then I compare each's distance from that particular checkpoint. The problem is that I do not know if this is efficient or not or if it is the right way to do it, and how/when I would call this function, because I obviously can't call this in update. Here is my code:
//calculate (sort)
Transform temp;
for (int i = 0; i < sortedRacers.Length-1; i--)
{
for(int j = 0; j < i; j++)
{
if(sortedRacers[j].GetComponent<LapCounter>().LAPCOUNT > sortedRacers[j + 1].GetComponent<LapCounter>().LAPCOUNT) //compare lap count
{
temp = sortedRacers[j];
sortedRacers[j] = racers[j + 1];
sortedRacers[j + 1] = temp;
sortedRacers[j].GetComponent<LapCounter>().Position = j+1;
sortedRacers[j+1].GetComponent<LapCounter>().Position = j+2;
}
else
{
if(racers[j].GetComponent<LapCounter>().currentCheckpoint > sortedRacers[j + 1].GetComponent<LapCounter>().currentCheckpoint) //compare which checkpoint they are on
{
temp = sortedRacers[j];
sortedRacers[j] = racers[j + 1];
sortedRacers[j + 1] = temp;
sortedRacers[j].GetComponent<LapCounter>().Position = j=1;
sortedRacers[j + 1].GetComponent<LapCounter>().Position = j + 2;
}
else
{
int currentCheck = sortedRacers[j].GetComponent<LapCounter>().currentCheckpoint;
Transform currentCheckTrans = sortedRacers[j].GetComponent<LapCounter>().checkpoints.GetChild(currentCheck);
if(Vector3.Distance(sortedRacers[j].position, currentCheckTrans.position) > Vector3.Distance(sortedRacers[j+1].position, currentCheckTrans.position))//compare distances to particular checkpoint
{
temp = sortedRacers[j];
sortedRacers[j] = sortedRacers[j + 1];
sortedRacers[j + 1] = temp;
sortedRacers[j].GetComponent<LapCounter>().Position = j+1;
sortedRacers[j + 1].GetComponent<LapCounter>().Position = j + 2;
}
}
}
}
}
Answer by gjf · Jul 08, 2020 at 09:18 PM
NOTE: None of this is tested but should give you an idea
Make sure that you have these at the top of the file:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Assuming that racers
is an array/`List` of Transforms
for your racers then your sort looks like this:
List<LapCounter> lapCounters = new List<LapCounter>();
for (int i = 0; i < racers.Length; i++)
{
LapCounter lapCounter = racers[i].GetComponent<LapCounter>();
if (lapCounter != null)
{
lapCounter.distanceFromCheckpoint = Vector3.Distance(racers[i].position, lapCounter.currentCheckpoint.position);
lapCounters.Add(lapCounter);
}
}
List<LapCounter> sortedRacers = lapCounters.OrderBy(x => x.LapCount)
.ThenBy(x => x.currentCheckpoint)
.ThenBy(x => x.distanceFromCheckpoint)
.ToList();
sortedRacers
now contains the sorted racers
Oh, that is so much more clear/ clean code! Thank you!
One more question however. Where/how would I call this method? It is not safe to call it in update right because it will be called 60 times a second. Is there any solution you can think of that I can use to call this assu$$anonymous$$g I put this code in a method?
If the number of racers is low then the performance hit won't be significant. How often you'd call it depends on what you're going to use the sorted list for. If it's UI, then updating twice a second might be enough.
Remember, it's not tested at all! ;)
PS. Ideally, you'd cache the LapCounter component reference somewhere and iterate over a list of something other than Transforms/GameObjects
to avoid the expensive lookup.
I am planning to implement about 8 racers. I am using this data for the UI yes. I am just worried about updating it twice a second is that if the opponent zooms by me, then there would be a noticeable delay. Thanks for the advice. I will try 4 times a second from a start and will work my way from there.