- Home /
How to display Car's Rank in a Racing Game
Hi,
I'm working on a Racing Game,I need to continuously show the Rank of MyPlayer. MyPlayer is running among other AI players (say 3 AI players in a level)
I have the following Details:
DistanceFromStartPoint[]; //distance of each player from starting point(updating all the time).
DistaceOfRace //Total Distance of Race.
could anyone let me know the algorithm or code to calculate/display MyPlayer Rank all the time during gameplay.
Thanks in advance !
if you're storing the distance from the start for all players, then the rank order is the distance traveled so just sort the distances (highest first)
will u please explain the distanceFromStart what is it...thankyou..
Answer by chetanisinanand · Apr 03, 2014 at 08:07 PM
I figured it out , and here is the solution :[ distanceFromStart(myPlayerIndex) is myPlayer's car distance from starting point ]
int Ranking( ) {
int rank = 1;
for ( int r = 1; r <= numberOfVehicles ; r++) {
if( distanceFromStart(myPlayerIndex) < distanceFromStart(r -1) {
rank++;
}
}
return rank;
}
Hi chetanisinanand,
Will you like to share the ranking script so others can learn from you. ?
Thanks in Advance.
Answer by Nilsboom · Aug 21, 2014 at 06:55 AM
I have solution that works in my Game. Define in Players Car Script
public Vector3 relativePosition = new Vector3 ();
public Transform Player; //Player Car
public Transform[] Target; // AI cars
public void Update() {
int numberOfFrontCars = 0;
for(int i = 0; i< 4 ; i++){
Vector3 relativePosition = transform.InverseTransformPoint (Target[i].transform.position);
// calculate relative pos of player car and AI cars . where Target is AI cars. Drag and drop your AI cars in Target Transform.
if(relativePosition.z < 0){
Debug.Log ("Front of AI ");
numberOfFrontCars++;
}
Debug.Log ("Current Rank :: "+(5-numberOfFrontCars));
}
Enjoy !!!You will get Rank of Your car....
Thanks but I am having some problem. If I change the direction of my car ranking starts to change with the direction.