# track how far player is from finish line

We are developing mobile racing game.We are looking to implement tracking system.

For example if four players are on race track then in up side of screen one slide bar type line will be there whcich will show how far user is from finish line.

Angry birds GO has done similar thing,Check in https://i.stack.imgur.com/YoAGy.jpg

Any suggestion on implementation ?

• learn this May 29, 2015 at 13:49

## 4 Answers

Assuming your track is not straight, a way to calculate the ratio:

1. Place invisible gates on strategic parts of your track, mainly in curves.
2. The total length of your track is the sum of the distance between each gate.
3. Each frame, given the last passed gate position to be Gl and the next gate position to be Gn, and the player position being P, D = |P - Gn| / (|P - Gl| + |Gn - P|) * (|Gn - Gl|), where D is the distance you calculate from the last passed gate.
4. You add D to the sum of the distances between all passed gate, lets call this T. T is the distance from the start of the track to the current point.
5. Divide T by the total length of the track and you get the current completed ratio of the player.

The odd calculation in 3 is to determine the ratio the player has to complete in the current gate span, and takes into consideration that the player could be off-track.

I'd say you probably want to trace out the track. Try setting up a script for the camera that adds the current position of the camera to a list every so many steps (AKA don't do it by time, but do it by distance.) Once you're done tracing the track you should probably dump a list of positions to a text file so that you can copy them and use them in-game.

The more positions (aka checkpoints) you have around your race track, the more exact the position bar will be.

Here's a picture for demonstration:

Sorry about the bad image quality. :P

EDIT: btw something just occurred to me - if your racing game has walls around the track edges then it won't be possible to cut through a section of grass. So if you're on two bends that come very close to eachother just using the distance formula will result in the game thinking that you are further ahead than you can possibly be. So to fix this, to get marker N, find the closest marker that is not more than 3 or 4 markers ahead of the last marker you were closest to.

Hope this helps!

• Thanks much, looks good solution too with almost no cost performance. But yes as Alexandre noted,it will not be smooth. May be I guess (n+ player's mean between current waypoint and next waypoint / m*100)% will do. May 29, 2015 at 17:18
• @idurvesh See my reply to Alexandre above. ;) May 29, 2015 at 17:42
• I see,thats good option too.Where performance matter one should go with this approach only. May 29, 2015 at 17:57
• I like how this process is easily completed manually without having to add entities to the map manually. The caveat, of course, is that the dev who drives the canonical lap needs to be very good at the game! Going off the course or "spinning out" will mess the system up. May 29, 2015 at 19:01
• @Kevin yes, of course, it does require the dev to run an almost perfect lap - but we're all gamers at heart. ;) besides if absolutely necessary, since speed doesn't matter, the dev could slow down the car / turn off collisions for the lap. May 29, 2015 at 20:35

SOLUTION

As posted algorithm in the other answer, here is the documented code flow of it,

Requirements,

Add one slider

Waypoints/checkpoints/gats holder script

Code,

Add following code in Coroutine,

float totalDistance;
int lastPassedWaypoint, upCoimngWayPoint;
float dis;
PlayerController playerCOntroller;
float TotalDistanceCovered,ratio;

IEnumerator TrackPositions(){

//getting total distance of track
for (int i = 0; i < Waypoints.Count - 1; i++)
{
totalDistance += Vector3.Distance(wayPoints[i].transform.position, wayPoints[i + 1].transform.position);
}
while (true)
{

if (upComingWayPoint == 0)
continue;

lastPassedWaypoint = upComingWayPoint - 1;

upCoimngWayPoint = Manager.upComingWayPoint; //get this from player manager script

//Use below multiline code if you need to track out of the track palyer position

/*dis = Vector3.Distance(this.transform.position, wayPoints[lastPassedWaypoint].transform.position) /
//  (  Vector3.Distance(this.transform.position , wayPoints[lastPassedWaypoint].transform.position) +
//         Vector3.Distance(wayPoints[upCoimngWayPoint].transform.position , this.transform.position) *
//        Vector3.Distance(wayPoints[upCoimngWayPoint].transform.position , wayPoints[lastPassedWaypoint].transform.position));
////totalDistance = wayPoints[WayPointsHolder.wayPointsHolder.wayPoints.Count].transform.position - wayPointsHolder.wayPoints[0].transform.position;
*/

//else use below line (optimized)
dis = Vector3.Distance(this.transform.position, wayPoints[lastPassedWaypoint].transform.position);

TotalDistanceCovered = 0;

for (int k = 0; k < lastPassedWaypoint ; k++)
{

TotalDistanceCovered += Vector3.Distance(wayPointsHolder.wayPoints[k].transform.position, wayPointsHolder.wayPoints[k + 1].transform.position);
}
TotalDistanceCovered += dis;
ratio = TotalDistanceCovered / totalDistance;

YOUR_SLIDER.value = ratio;

yield return new WaitForSeconds(0.02f);
}


All players move according to the x-axis. Your target has a constant x position. You can know how far they're from the finish line by knowing the value of Target.X - Player.X. If it equals zero, he's arrived.

• Hi, the track is not straight so how to go about it?As X and Z both will take into consideration.. Its 3D game May 29, 2015 at 14:32
• -1, that won't work for a two dimensional track. I don't know many racing games where the track is a straight line... well I think you may have misunderstood the question. May 29, 2015 at 14:58
• @idurvesh see my answer for an approach you could take. May 29, 2015 at 14:59
• @Superdoggy the example he gave me was for Angry Birds, yes, I agree your answer is the one for tracks such as that, but I'm afraid that's not a 2D-side game, but it's a top-down game. In a 2D game, you will almost always refer to my solution.
– Zee
May 29, 2015 at 18:39
• If this was the case, I don't think the asker would need to ask this question. Difference between A and B? A - B. May 29, 2015 at 19:03