- Home /
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;
}
}
}
}
}
Please format your code correctly, if you don't know how, watch the tutorial video on the right :)
Hi phaninder15,
Will you share the script for calculating rank for cars in racing game..? Any help is appreciated.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613120106im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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