0

I think I might just be trying to come up with some form of magical solution to a problem that requires the use of a different data structure in order for it to work but here goes..

I'm currently making a racing game and I want a live update of the player's current position. To do this I am tracking two things, their current checkpoint count (the number of checkpoints they have passed through) I then check the distance between each car and the next checkpoint they are heading towards.

When the check position function is called (every second) the cars number of checkpoints are placed in an array so [0] in the array will be car1 etc and the distance from each car and the next checkpoint they are heading to is also in an array so [0] would be car1's distance to their next checkpoint.

What I need is a way to sort the two arrays in tandem so that they still relate to the same car when i access an element from either, the problem however is I seem to be stuck, I've used Array.Sort(,) and placed the two in there so that they align but say I have 3 cars on 2 checkpoints and the rest on 1 the cars with 2 checkpoints are not sorted by distance.

Is there anything you guys can think of that can fix this. I've tried to avoid using getcomponent etc so ive made the position script access static variables that relate to each individual cars number of checkpoints and distance to next CP, therefore I tried to avoid classes and what not.

I feel like I'm 80% of the way there so its annoying asking for help.

4

1 Answer 1

1
  • Make an object containing both distance and checkpoint count.
  • Do CarList.Sort((x, y) => (your sorting logic here) OR Implement IComparable for that object and call sort

You will maintain one list and you logic of sorting or comparing cars will be hidden in each object.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.