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! :)
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).
Answer by xToxicInferno
Aug 20, 2010 at 03:15 AM
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.
How do you write the code to check for the lap and the checkpoints? Any examples?
Answer by Herman Tulleken
Aug 20, 2010 at 01:56 PM
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) /
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:
Answer by POLYGAMe
Nov 17, 2011 at 09:44 AM
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.
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.
@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
I think you want a different Karl @edwar, not me :)
@karljj1 ...yess :D
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.
Answer by lakshmi8
Nov 17, 2011 at 12:15 PM
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...
Only the positions need to be updated dynamically, not just at the end of every lap.
Answer by bernadette
Aug 21, 2010 at 11:58 AM
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! :)
Novodantis already commented on your last answer - use comments to discuss, rather than answering your own question with more questions!
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
5 People are following this question.
Does anyone have figured the WheelFrictionCurve struct ?
Comparing values of an array
How to make car accelerate/decelerate over time
Problems with a racing game level.
I want to make a tap to "teleport" code.. is this correct?