- Home /
Ranking System in Racing Game
Hey guys,
This topic has been driving me up the walls and has shown me that I really don't quite understand how arrays, lists, etc. work. I've already been trying to google for WEEKS (!!) now in order to find out how to do this kinda stuff properly, but this challenge has been way over my head so far.
So, here's the design: I have a racing game and I want to always track the players positions. Every car has 3 variables:
lap = The lap the player is on checkpoint = The number of checkpoints the player has passed distanceToCheckpoint = The distance to the next checkpoint.
In terms of logic, I'm trying to sort it all in this order: Lap -> Checkpoint -> Distance.
So first I have to check over the laps the cars are on. But if they're on the same lap, I have to check which checkpoints they're on. But if they're on the same checkpoint, I have to check which car is the closest to the next checkpoint.
I've been checking Unity Answers, the Unity Forums and some other places and since Javascript now supports Lists, I think most people suggest that lists should be used for something like that.
My issue is that this is really goddamn complicated, since I constantly have to Compare the variables within the arrays / lists between 4+ cars and then figure out who's 1st, who's 2nd, who's 3rd, etc.
I found out that I can use a Compare Function like this:
function CompareCheckpoints(carP1: GameObject, carP2: GameObject): int
{
var scriptA = carP1.GetComponent(carSettings);
var scriptB = carP2.GetComponent(carSettings);
if (scriptA.checkpoint > scriptB.checkpoint) return -1;
if (scriptA.checkpoint < scriptB.checkpoint) return 1;
return 0;
}
...in order to find out which car is in the first place based on its checkpoint var. Now, this works nicely for 2 cars, but I'd have to do a whole lot of > and < in order to find out the differences between multiple cars.
So I kinda think I know how this could be done in a very, very complicated way, but I'm hoping someone here already coded something like this and could give me a hint on how to go about it in terms of logic / code, preferably in Javascript ;)
Answer by fafase · Mar 02, 2015 at 01:49 PM
Well, trying not be condescending but if you think games are easy to make because they are easy to play, then ...well....
The way you describe is pretty much the way to go. What you consider complex is not and you seem to have the correct thinking as well so just go on with it.
Get the current lap and order your cars
Get the current waypoint and order again
Get the closest to the waypoint and final order
No easier way I can think of that would not take advanced complex algorithm to gain that little nano second you can't care less about, and this is pretty efficient too since it is just a bunch of arithmetic computation. One thing you can do is run the method only at lower frequency, like 2 times per second or even once per second as it is enough to keep track of position.
One thing you could do is optimise your ordering with binary search for instance.
EDIT based on comments:
Each car has a script that keep track of the lap and waypoint You could have a value that is :
counter = lapCount * 1000 + waypointCount * 100 + distanceToPreviousWaypoint;
The you have LapTracker that gather all cars into a list or array and order by that value
m_targetList.Sort(delegate(GameObject c1, GameObject c2){
return c1.GetComponent<Tracker>().counter.CompareTo
(c2.GetComponent<Tracker>().counter));
});
Note thsi is only valid if the distance between waypoint is less than 100 units. If more than that then you need to change the multipliers for 10000 and 1000 to make it consistent.
Oh, I made quite a few games before, but I'm a lowly designer, not a programmer :)
Here's the prototype I'll use this for:
http://www.warsoup.com/RacingGame/RacingGameWeb.html
Your answer is just kinda confir$$anonymous$$g what I've been asking, but it's not really helping me yet. I'm wondering how I'd go about that using a Generic List, for example. How do you write the code for:
'Check all the lap variables of these objects, if there are 2 more that are the same, check checkpoints (while you can already assign the rank variable to the others), then if they're the same, check distance'.
I don't think I've been doing a shit job on coding the rest of it, but I'm definitely a novice programmer, if that. I just need a little help on how to deal with arrays / lists and how to make these queries in the proper syntax. I've been already spending quite some time trying to figure it out, but I'm still not there yet.
I tried this now, but it's giving me a whole bunch of errors:
Is this Javascript or C#?
Answer by Mmmpies · Mar 02, 2015 at 02:33 PM
Or you could do it with trigger colliders.
Stretch a trigger collider across the start line. Have the car store the lap its on and if the car is the first one to hit the trigger tell the leaderboard that car is first for that lap.
Say you got four cars and one is in the pits. 3 cars are on lap 3 but when that car hits the start line increment the lap to 1 for that car then check its position for lap 1. It'll be fourth.
Put triggers on each way point just use the same logic without incrementing the lap.
Youll have to watch out for cars going off and going through a trigger twice but you only really need to set it to the same value as the last car without incrementing the number of cars that have gone through that trigger.
Sorry for typos, on my phone!
Your answer
Follow this Question
Related Questions
About "How do I make laps in a racing game?" 2 Answers
Letter Ranking System, Checking Higher Rank 1 Answer
How to create a ranking system for a racing game. 0 Answers
ranking in race Game 2 Answers
Enter / Exit Trigger 2 Answers