- Home /
Setting up player positions in a racing game
Hi everyone,
I've been trying to setup player positions in game. Been all over the forums and managed to get this far but just need a little help with the last step.
I've set up a race course that has a series of checkpoints. These checkpoints are just to track the players position through the track and make sure they're at the right point and stop them incrementing the lap counter by just going over the line continuously.
So every racer has
public static int currentCheckpoint = 0;
public static int currentLap = 0;
The checkpoints are triggers that increment the currentCheckpoint int and when a player successfully goes through the every waypoint currentCheckpoint is set to 0 and currentLap is then incremented.
My issue is now setting up something that'll allow me to work out the position of each player.
Just needed a little assistance in how I should sort this information. I was thinking an array? That would just constantly be sorted depending on the player's checkpoint and lap count.
I've managed to set up a system that stores the distance of each racer to the next checkpoint they are due to clear so this would be another variable I'd have to take into account.
I'm pretty sure the logic behind the code would be putting each of these variables into an array and then Sorting the Lap array to find out which lap players are in and then sorting the current checkpoint array and then finally the distance to waypoint array.
Any advice on this would be greatly appreciated.
Thanks!
EDIT: I was thinking about turning the player's position information into one int. So let's say there were 10 checkpoints. A int higher than a 1000 would indicate them being on the second lap and so on. And I was thinking about converting the distance into a percentage so it would always be out of a 100 and add up to 1000...
Bad idea?
EDIT 2: So I've managed to set up some arrays which keep track of the lap of the player and of the checkpoint they're on. But in terms of ordering them I'm a little lost. I could sort them but because the array's only hold the checkpoints and laps as separate integers there's no index to reference which number belongs to which player once they've been sorted. I was hoping to make them multidimensional so I could have the actually player's object as the index to relate to the lap and checkpoint to sort them. The problem I'm not too sure how to make a multidimensional array with different types... Can arrays even do this or would I need a list or something else?
Any input or direction here would be much appreciated.
Ummm, each player can't have a static int - that's shared by all players.
EDIT: Completely misread your comment lol but thanks for the heads up. I've only got 1 racer atm so this will change lol!
What you propose in your edit to the questions makes sense to me. If ins$$anonymous$$d of the lap being 1000 units, it was, say 1200metres, then your int could actually measure how far in metres the player has travelled. Or, use floats, and have one lap as 1 unit, hence three quarters of the way around lap 5 would be 4.75. Either way you're measuring distance, either in metres or laps. Then, as you say, you have an associative array with keys being the player number (player1, player2, etc) and the values being the distance they have travelled. Sort by key to get a list sorted by player, and by key to get a current ranking.
Right, this is going horribly. I've managed to set up arrays holding the information separately but like you said I'm going to need a key/index so it's going to have to be multidimensional array which I'm not too sure how to set up. It'd mixed type array I think so I could put the player's gameobject in, or maybe I could just make a unique key and do it that way. From there it'd just be sorting and displaying the information. I've tried Google but the examples aren't too clear. Would anyone $$anonymous$$d posting an example? It'd really help clear things up.
Answer by kidshenlong · Mar 29, 2013 at 07:25 PM
So after scouring the net and getting help from a few different places I've come to a resolution.
Lists.
I put all the player objects into a list and then by using linq I then order the list by accessing the progress int which is the value that represents the player's progress through the course.
myList = myList.OrderByDescending(x => x.GetComponent<CarCheckpoint>().progress).ToList();
Thanks for all your help...
I do still have one issue though. When the player goes through a checkpoint it updates the progress integer. This is ineffective though as the player's positions are only updated when crossing through a checkpoint.
As I discussed above I do have bit of code that measures distance between each checkpoint. This is then turned into a percentage. So if a player is half way to the next percentage it'll be 50% obviously...
I'm not sure how to word this but I'm hoping to add the percentage as an integer onto the progress int but instead of constantly incrementing this number only to add the difference on each time.
just to clarifying:
If player one's position is the 50% between checkpoint 2 and 3. The progress should be at 250, but when they get to 55% the progress integer should be at 255. So I'm looking to just add on the difference rather than adding 50 then 55 and ending up with 305 or some other ridiculous number.
Found it difficult to word that so please reach out if this isn't making any sense lol
Hi kidshenong,
Will you please share the script to calculate the ranking of car in racing game. Any help will be appreciated. Regards.
Your answer
Follow this Question
Related Questions
How to add progress bar showing player movement from start to finish line in 3d game ?, 1 Answer
How to make a Race Standing / Position Indicator 0 Answers
Camera rotation around player while following. 6 Answers
Racer positioning system and race end 0 Answers
players positions when race ends. 4 Answers