Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gugu6897 · Jun 04, 2013 at 02:37 AM · distancesystemtrackrace

How to build a lap system for racing game

I already have a checkpoint system in my racing game. But I want to accurately calculate the distance in any of the following examples/methods: alt text

Example 1 (see the image): Track distance between two checkpoints.

Example 2 (see the image): Track distance between distinct checkpoints.

Example 3 (see the image): Distance to be traveled in the entire track(lap).

I want to use this distance to calculate the player position.

Can anyone guide me on any of these methods.

Note: I don't want to calculate the linear distance between the checkpoint objects, but I need to calculate the distance traveled on the track.

Thank you for your attention and I would be very grateful for any help.

God bless you.

lap system.jpg (207.4 kB)
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 04, 2013 at 05:00 AM 0
Share

it's by no means easy to do this. what does "distance on the track" mean?

do you mean "progress" .. how far from the finish line .. or how far the car has literally travelled? which wheel? etc.

it's tricky

avatar image gugu6897 · Jun 04, 2013 at 04:00 PM 0
Share

Thx for your attention. I want to accurately calculate "how far" is the car from the finish line. I want to use this to check the player position (first, second, third,...).

Can you help me?

I am very grateful. (I am brazilian and my english is basic)

avatar image Fattie · Jun 04, 2013 at 04:02 PM 0
Share

ciao !

it depends if you measure on the inside or outside of the track you know?

avatar image gugu6897 · Jun 04, 2013 at 04:18 PM 0
Share

I want to calculate/get the distance from the finish line inside the track, like the green line in the image.

avatar image Fattie · Jun 04, 2013 at 04:20 PM 0
Share

that's the CENTERLINE of the track -- right ?

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by gugu6897 · Jun 04, 2013 at 08:10 PM

Explaining my image:

•The blue cubes are CP (Checkpoints/Waypoints)

•The green line is just drawing the track progress.

Explaining the CP system:

•When I pass a CP (trigger), a 'counter' storage my current CP by a 'int'.

•When this counter reach the maximum number of CPs in the track, a var named 'lap' get a increment.

•I check the current CP and calculate the distance between the car and the CP using 'Vector3.Distance'

Conditions:

 if('Opp lap'   >   'My lap')
 {
     //I'm in second place
 }
 else if('Opp lap' == 'My lap'  &  'Opp CP' > 'My CP')
 {
     //I'm in second place
 }
 else if('Opp CP' == 'My CP' && 'Opp distance' < 'My distance')
 {
     //I'm in second place
 }
 else
     //I'm in first place


'My Lap' = My current lap

'My CP' = My current checkpoint

'Opp lap' = Opponent current lap

'Opp CP' = Opponent current checkpoint

'My distance' = Distance of the player between next checkpoint

'Opp distance' = Distance of the opponent between next checkpoint


This is working fine with two drivers/racers. I'm working now to get this working with more drivers/racers.

I hope I have helped you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Nomibuilder · Apr 08, 2015 at 07:18 AM

If you are using C# Language. Then here is the C# version of CheckPoint and Laps system.

Laps Script

 using UnityEngine;
 using System.Collections;
 
 public class Laps : MonoBehaviour {
     
     // These Static Variables are accessed in "checkpoint" Script
     public Transform[] checkPointArray;
     public static Transform[] checkpointA;
     public static int currentCheckpoint = 0; 
     public static int currentLap = 0; 
     public Vector3 startPos;
     public int Lap;
     
     void  Start ()
     {
         startPos = transform.position;
         currentCheckpoint = 0;
         currentLap = 0; 
 
     }
 
     void  Update ()
     {
         Lap = currentLap;
         checkpointA = checkPointArray;
     }
     
 }


Check Point Script

 using UnityEngine;
 using System.Collections;
 
 public class checkpoint : MonoBehaviour {
     
     void  Start ()
     {
 
     }
     
     void  OnTriggerEnter ( Collider other  )
     {
         //Is it the Player who enters the collider?
         if (!other.CompareTag("Player")) 
             return; //If it's not the player dont continue
         
 
         if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform) 
         {
             //Check so we dont exceed our checkpoint quantity
             if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length) 
             {
                 //Add to currentLap if currentCheckpoint is 0
                 if(Laps.currentCheckpoint == 0)
                     Laps.currentLap++;
                 Laps.currentCheckpoint++;
             } 
             else 
             {
                 //If we dont have any Checkpoints left, go back to 0
                 Laps.currentCheckpoint = 0;
             }
         }
 
 
     }
 
 }


And Just Follow this tutorial to attach scripts on your Game Objects. Link

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nomibuilder · Apr 15, 2015 at 07:34 AM 1
Share

Hope this would be helpful for C# Users :)

avatar image MoonR1d3r · Jan 30, 2018 at 02:15 AM 0
Share

there was an error in the code, which disabled the whole thing to work, change (!other.CompareTag("Player")) to (other.CompareTag("Player")). There should be no exclamation point.

avatar image Silveralby · Oct 21, 2019 at 02:40 PM 0
Share

$$anonymous$$oonR1d3r if I remove the exclamation point I get this error: NullReferenceException: Object reference not set to an instance of an object checkpoint.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/checkpoint.cs:20)

avatar image
0

Answer by LVBen · Jun 04, 2013 at 02:48 AM

initialize a variable (like 'distanceTraveled') to 0. On every update, find the distance between the last position and the current position, store the current position into a last position var, and add the distance to distanceTraveled.

You could have multiple variables for each distance that you want to track.

distanceTraveledFromStart

distanceTraveledFromLastCheckPoint

distanceTraveledThisLap

etc...

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HypoXic5665 · Jun 04, 2013 at 04:20 PM 0
Share

One issue I see with this solution is that the player place (1st, 2nd, 3rd ect) that gugu wants to track could be inaccurate. Say the user travels in a wavy line (or even travels backward) the physical distance traveled could be greater than a user that is actually in front.

That being said I do not have a better solution at the moment. I would be interested to see a solution that does not encounter this problem.

avatar image LVBen · Jun 04, 2013 at 06:36 PM 0
Share

Assu$$anonymous$$g that the track is a 3d object and he has vertices along the outside and inside edges, then he can get the lengths of the inside and outside edges along those vertices. Adding them together and dividing by two would give the length through the center of the track.

avatar image kalyan11 · Dec 14, 2015 at 06:04 AM 0
Share

Hey how set laps......please help me....

avatar image
0

Answer by Pangamini · Oct 21, 2019 at 06:20 PM

What I'd probably do would be to define the track with some loop line. I'd calculate the position on the line by looking for the closest point of the car to the line. Here you'd also need some way how to prevent discontinuities (when your car goes off the track and closer to some other part of the loop, by somehow "dragging" the control point over the line, like it was a rail.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to create race lap timer, start to finish, display gui? 1 Answer

Race checkpoint system small help please :) 1 Answer

Accessing local system ( File Browser ) 2 Answers

How do I make laps in a racing game? 5 Answers

Magnetic Race Track? How to stick to a tubular track? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges