Before updating to Windows 10 you may need to return your Pro license. More information here

Race Position HELP!!

we created a racing game,, but it seems there a problem in positioning.. like (1st, 2nd, 3rd.. etc.) Can you help up to find some way to finish our project?? Please ?? :( thanks! :)

more ▼

asked Aug 17, 2010 at 07:22 AM

avatar image

20 6 4 5

You need to give some more information. Is your problem determining who finishes 1st, 2nd, etc., or having that information during the race? Also, how do you specify the track (typically zones or curves are used).

Aug 17, 2010 at 10:39 AM Herman Tulleken
(comments are locked)
10|3000 characters needed characters left

8 answers: sort voted first

I actually learned how to do this from a video on YouTube (on GyroVorbis's Channel but it was Dual Threat who said it), the way that team did it was to set up 'checkpoints' along the track. Then to determine the position of the racers is in three steps: What lap you on. What checkpoint you on. Whats your distance to the next checkpoint. This meaning if you are on your final lap, and everyone else is on the 2nd lap you are in the lead. However, if you and someone else are on the same lap, but you are half the a track ahead of them, then you can tell by what checkpoint you are on. Now if you are both neck and neck with each other, then who ever was just SLIGHTYLY ahead of the other will be in the lead. Here is the link to the video explaining it.

YouTube Video: Go to 12:55 for the info you are looking for, but also check out the other stuff they got, it is really good.

Lastly, all credit for this idea goes to Dual Threat Studios.

more ▼

answered Aug 20, 2010 at 03:15 AM

avatar image

560 59 61 69

How do you write the code to check for the lap and the checkpoints? Any examples?

May 20 at 10:52 AM SwagGamez
(comments are locked)
10|3000 characters needed characters left

Like xToxicInferno said, you use the current lap and last checkpoint visited for broad comparisons. When two vehicles are between the same node-points, you can calculate the following for each vehicle---it gives the fraction of that piece of road covered (0 means you are at the first checkpoint, 1 means you are at the second checkpoint).

public float GetFractionOfPathCovered(Vector3 position, Vector3 lastNodeReached, Vector3 nextNode) { Vector3 displacementFromCurrentNode = position - lastNodeReached; Vector3 currentSegmentVector = nextNode - lastNodeReached; float fraction = Vector3.Dot(displacementFromCurrentNode, currentSegmentVector) / currentSegmentVector.sqrMagnitude;

     return fraction;


This essentially calculates the length of the projection of the vehicle's position on the line segment between the checkpoints before and after the vehicle. (You can see this article that will explain more on the maths of vectors and projections).

You can compare these values - the vehicle with the larger one is in front.

You can also use this to go from one checkpoint to the next - as soon this value is larger than 1, you know that the vehicle reached the next checkpoint.

A few things to watch out for when you use checkpoints:

  • The method does not work well when the angle between consecutive segments is 90 degrees or smaller. For sharp bends, you need to add a lot of checkpoints to prevent this.
  • For the last reason, you will need a good tool / system to place check-points and set up links between them, especially if you intend to support shortcuts / alternate routes.
  • You need to carefully consider how you will handle players driving backwards (of course, everyone knows that is not part of a racing game, however, it's a very obvious bug when not addressed). Ideally, you will need to to decrease checkpoints and lap counts for vehicles going in reverse. (You can use the above test as well - when the value goes below 0, you know the vehicle is past the last checkpoint in the reverse direction). This significantly complicates the logic and data you need to keep track of for each vehicle.
  • Vehicles driving head to head might sometimes cause the race positions to flicker between two positions. Although this is "correct" in the sense that this value is calculated, it looks like a bug. You might want to use a buffering scheme that only changes the displayed racing position after it has remained consistent for some number of frames or some time interval (but not too long!)
more ▼

answered Aug 20, 2010 at 01:56 PM

avatar image

Herman Tulleken
2k 69 85 126

(comments are locked)
10|3000 characters needed characters left

The way we did it for our project is have LOADS of empty game objects as waypoints (that cover the entire width of the track, from boundary to boundary, so cars cannot miss them)... it's brute-forcing it, but it works a charm. Basically, each car has a counter, as it passes a waypoint, we add one to the counter... so the player with the highest counter at any one time is in the lead and so on. Simple but very effective ;-)

Obviously you'll need to take into account driving the wrong way down the track etc, but this was the most simple and effective way we could come up with. We did it with a whole bunch of different mathy things too but still found issues when it comes to going around corners etc.

more ▼

answered Nov 17, 2011 at 09:44 AM

avatar image

1.1k 147 121 145

I used this approach in my game too. As my track had lots of tight corners and close turns.. so using distance and vectors was pretty much useless and open up to bugs. Doing it with cubes and box colliders around the track is the most foolproof way. Although my implementation is different from yours in that I named each collider, from 1 to say 100. Then the cars progress was calculated by the number of laps * the number of colliders + the number collider they were on for their current lap.

May 01, 2012 at 06:29 PM Meltdown

@POLYGAMe Can i ask you for sample code... sorry i have no right to ask you but this well really help a lot Sir :D

Jul 30, 2012 at 07:21 PM Dee Va

I think you want a different Karl @edwar, not me :) Try @POLYGAMe

Jul 30, 2012 at 07:38 PM karljj1

@karljj1 ...yess :D

Jul 31, 2012 at 10:46 AM Dee Va

Haha, our positioning script is tied in with a bunch of other stuff including all the waypoints etc, loads of different scripts for different car behaviours, so it wouldn't be of much help, sorry.

Jul 31, 2012 at 11:13 AM POLYGAMe
(comments are locked)
10|3000 characters needed characters left

Easiest way would be creating a collider at the finish line and calculate the no. of hits depending on the tag name.

If you have same track and 4 laps in that then the car which touches the collider firstly for the fourth time wins and in the same way you can get all the positions...

more ▼

answered Nov 17, 2011 at 12:15 PM

avatar image

-9 3 2 3

Only the positions need to be updated dynamically, not just at the end of every lap.

Nov 17, 2011 at 08:21 PM POLYGAMe
(comments are locked)
10|3000 characters needed characters left

thanks mr.xtoxicInferno and sir Herman Tulleken.. But, may i ask how to create a checkpoint? Can you share to me the codes for it?? thanks God BLess! :)

more ▼

answered Aug 21, 2010 at 11:58 AM

avatar image

20 6 4 5

Novodantis already commented on your last answer - use comments to discuss, rather than answering your own question with more questions!

Aug 21, 2010 at 01:37 PM Marowi
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Aug 17, 2010 at 07:22 AM

Seen: 7686 times

Last Updated: May 20 at 08:50 PM