- Home /
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:
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.
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
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)
ciao !
it depends if you measure on the inside or outside of the track you know?
I want to calculate/get the distance from the finish line inside the track, like the green line in the image.
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.
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
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.
$$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)
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...
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.
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.
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.