Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by toishaanpatel · Jul 08, 2020 at 09:19 PM · car gameracepositions

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;
                         }
                     }
                 }
             }
         }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toishaanpatel · Jul 08, 2020 at 09:48 PM 0
Share

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?

avatar image gjf toishaanpatel · Jul 08, 2020 at 10:37 PM 0
Share

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.

avatar image toishaanpatel gjf · Jul 09, 2020 at 01:41 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RaceGame - Car Follow Mouse 1 Answer

Cannot make initial speed for the Player car using AIDriverToolkit 0 Answers

How do I make use of an Import and make use of a downloaded race track? 2 Answers

Player Race Position 2 Answers

How Do I Set The Number Of Laps? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges