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 bernadette
Aug 20, 2010 at 02:31 AM
yes, to determine who finishes 1st, 2nd, 3rd.. i mean when in an actual race track, when an AI comes behind you, typically we say that your in first position, when AI runs out in your front, you'll become in 2nd position.. what we will do?
Please use comments if you wish to discuss; only use Answer when answering a question. Thanks :)
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 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!
Answer by Alejandro Ravasio
Nov 19, 2010 at 01:24 PM
I don't know if this is of any use to you, but a waypoint is, at least on my project, nothing more than another game object dropped into the scene. You just use it's transform.position property as a reference point.
vector3 waypointPosition = GameObject.Find("nameOfYourObjectWorkingAsWaypoint").transform.position;
should give you a vector3 to work with. Or you can just skip the waypointPosition variable and use the whole function, but I'd advice against that, considering it's a paint to read it.
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 site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
6 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?