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 !
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*.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…
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;
}