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 phaninder15 · Oct 17, 2013 at 08:11 AM · programmingracingrace

Rankings in racing Game

Hi all, am currently working on a Racing game. I have implemented almost everything but unable to find the rank of cars. Currently i am calculating the no of checkpoints covered by each and calculating ranks based on the checkpoints covered.If any two cars cover same no of checkpoint then am calculating the distance between the car current position and next checkpoint and calculating the car's rank. but some how it unable to get the exact rank. am enclosing the scripts please guide me on how to solve this.

This script is attached to every car and it calculates checkpoint count and distance between the car and next checkpoint.Initially i have set the rank to no of cars for all cars using UnityEngine; using System.Collections;

 public class CheckpointScript : MonoBehaviour 
 {
     public static Transform playerTransform;
     
     void Start()
     {
         playerTransform=GameObject.Find("fullCarCollider").transform;        
     }
     
     void OnTriggerEnter (Collider other)
     {
         //Is it the Player who enters the collider?
         if (!other.CompareTag("Player")) 
         {
             return; //If it's not the player dont continue
         }
         
         //Is this transform equal to the transform of checkpointArrays[currentCheckpoint]?
         if (transform == playerTransform.GetComponent<CarCheckPointScript>().checkPointArray[CarCheckPointScript.currentCheckpoint].transform) 
         {
             //Check so we dont exceed our checkpoint quantity
             if (CarCheckPointScript.currentCheckpoint + 1<playerTransform.GetComponent<CarCheckPointScript>().checkPointArray.Length) 
             {
                 //Add to currentLap if currentCheckpoint is 0
                 if(CarCheckPointScript.currentCheckpoint == 0)
                     CarCheckPointScript.currentLap++;
                 CarCheckPointScript.currentCheckpoint++;
             } 
             else 
             {
                 //If we dont have any Checkpoints left, go back to 0
                 CarCheckPointScript.currentCheckpoint = 0;
             }    
             playerTransform.GetComponent<CarCheckPointScript>().checkpointCounter++;
         }
         
     }
 
 }

This script calculates the rank of the cars using UnityEngine; using System.Collections;

 public class RankgSystem : MonoBehaviour 
 {
     public Transform[] obj;
     public CarRankingScript[] tempObj;//for referencing the carranking script
     // Use this for initialization
     void Start () 
     {
         tempObj=new CarRankingScript[obj.Length];
         for(int i=0;i<obj.Length;i++)
             tempObj[i]=obj[i].GetComponent<CarRankingScript>();  //getting the carranking script and storing it in the temp object
     }
     
     // Update is called once per frame
     void Update () 
     {
         //calculating the ranks of the cars based on the checkpoint count
         
         //we are going throught each array object and comparing the checkpoint count with the next one and if we found one checkpoint is greater than other 
         //we are decreasing the rank by one and keeping the other car rank same i.e its previous rank
         //we are not checking for equal checkpoints in this loop
         for(int i=0;i<tempObj.Length-1;i++)
         {
             for(int j=i+1;j<tempObj.Length;j++)
             {
                 //if both cars hvae same checkpoints
                 if(tempObj[i].checkpointCounter == tempObj[j].checkpointCounter)
                 {
                     //compare the distance from next checkpoint
                     if(tempObj[i].distanceFromNextCheckpoint<tempObj[j].distanceFromNextCheckpoint)  //if 1st car distance is less then 2nd car we decrease
                     //1st car rank and increase 2nd car rank
                     {
                         if(tempObj[i].rank>1)   //decreasing the rank of the more checkpoints and increasing the rank of less checkpoint
                         {
                             tempObj[i].rank-=1;
                         }
                         if(tempObj[j].rank<tempObj.Length)
                                 tempObj[j].rank+=1;
                     }
                     else
                     {
                         if(tempObj[j].rank>1)
                         {
                             tempObj[j].rank-=1;    
                         }
                         if(tempObj[i].rank<tempObj.Length)
                                 tempObj[i].rank+=1;
                     }
                 }
                 
                 if(tempObj[i].checkpointCounter > tempObj[j].checkpointCounter)  //checking the no of checkpoints in each objects
                 {
                     if(tempObj[i].rank>1)   //decreasing the rank of the more checkpoints and increasing the rank of less checkpoint
                     {
                         tempObj[i].rank-=1;
                     }
                     if(tempObj[j].rank<tempObj.Length)
                             tempObj[j].rank+=1;
                 }    
                 else if(tempObj[i].checkpointCounter < tempObj[j].checkpointCounter)  //if t is less than
                 {
                     if(tempObj[j].rank>1)
                     {
                         tempObj[j].rank-=1;    
                     }
                     if(tempObj[i].rank<tempObj.Length)
                             tempObj[i].rank+=1;
                 }
         
                 
             }
         }
         
         
     }
 }
 
Comment
Add comment · Show 2
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 Benproductions1 · Oct 17, 2013 at 08:17 AM 0
Share

Please format your code correctly, if you don't know how, watch the tutorial video on the right :)

avatar image abdulthegamer · May 21, 2014 at 10:28 AM 0
Share

Hi phaninder15,

Will you share the script for calculating rank for cars in racing game..? Any help is appreciated.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by phaninder15 · May 23, 2014 at 05:36 AM

I can't give you script but i can say you the logic behind it.

I have placed checkpoints all around the track and i will compare the distance from car to its next checkpoint.There are three cases 1.Check the lap the player and opponent cars are in(i updated lap if player or opponent reaches the starting checkpoint again),the higher lap guy has lesser rank. 2.If both or all cars are in same lap,check the next checkpoint they are going for,after a car reaches a checkpoint my script say the next checkpoint it need to reach(all checkpoints are given a no so i can easily find out which checkpoint is higher). The Car on the higher checkpoint is of lesser rank. 3. If both or all cars are on the same checkpoint then check the distance for all car to the next checkpoint, the car with less distance is of lower rank(1,2,3...).

Hope that will be helpful

Comment
Add comment · Show 1 · 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 One-Zero · Mar 02, 2016 at 06:37 AM 0
Share

Thanks for sharing your Idea

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

18 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

Related Questions

Multiple Cars not working 1 Answer

How to display Car's Rank in a Racing Game 3 Answers

AI Car Problem 1 Answer

Problem getting vehicles to stick to the road in an F-Zero style game 1 Answer

AI car Script problem 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