# 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_=obj*.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*.checkpointCounter == tempObj[j].checkpointCounter)
{
//compare the distance from next checkpoint*
if(tempObj*.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.rank>1) //decreasing the rank of the more checkpoints and increasing the rank of less checkpoint*

* {
tempObj.rank-=1;
}
if(tempObj[j].rank<tempObj.Length)
tempObj[j].rank+=1;
}
else*

* {
if(tempObj[j].rank>1)
{
tempObj[j].rank-=1;
}
if(tempObj.rank<tempObj.Length)
tempObj.rank+=1;
}
}*_

_ if(tempObj*.checkpointCounter > tempObj[j].checkpointCounter) //checking the no of checkpoints in each objects*
* {
if(tempObj.rank>1) //decreasing the rank of the more checkpoints and increasing the rank of less checkpoint*

* {
tempObj.rank-=1;
}
if(tempObj[j].rank<tempObj.Length)
tempObj[j].rank+=1;
}
else if(tempObj.checkpointCounter < tempObj[j].checkpointCounter) //if t is less than*

* {
if(tempObj[j].rank>1)
{
tempObj[j].rank-=1;
}
if(tempObj.rank<tempObj.Length)
tempObj.rank+=1;
}*_

* }*
* }*

* }*
}

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…).