How do you create a racing standing/positions in a racing game? I have looked all over answers and forums but all I saw that to do it you create a checkpoint and lap system. Then, determine the position based on which check point and lap each car is on. However, when the cars are on the same checkpoint, find the distance between the cars and the next checkpoint and order the car’s position. This is the part I don’t get. I looked all over answers and forums, tried copying and pasting scripts from them but those don’t work. Also, I tried writing my own scripts and I failed miserably. What I really want is not a script but a video tutorial to teach me how to a make racing positions system. I know that there are items in the asset store to help me but they are too expensive. So how do you create a racing positions system by hand?
I want to create a position indicator like this
since you know what checkpoint has been passed for each car simply sum all the distances up to the finish line for each. The one with the shorter distance is ahead of the others, simply sort the list by that.
Things you need to calculate first:
- how far is my car from the next checkpoint?
- how far is my car from the finish line (ie adding all checkpoint distances together)?
There’s probably easier ways but this is not an expensive procedure, particularly as all those distances can be precalculated (ie checkpoint knows distance to next).
When you have that data you can just sort a list by total distance.
googled “unity race positions”
3rd from the top: Race Position HELP!! - Questions & Answers - Unity Discussions
seems like some fairly straight forward descriptions of the concepts involved
@hippocoder and @LeftyRighty thank you for the information. However, I am having a great struggle calculating distances for some reason. I tried using Vector3.Distance but it is not working. What should I do?
P.S Other than trying to write my own distance code(which sadly did not work) I tried copying and pasting code from the answers section but for some strange reason their code did not work either.
Some Code:
playerDistance = Vector3.Distance (Laps.checkpointA [Laps.currentCheckpoint].transform.position , player.position);
enemyDistance=Vector3.Distance(Laps.checkpointA [Laps.currentCheckpoint].transform.position , enemy.position);
if (playerDistance < enemyDistance)
pos = 1;
else if(playerDistance > enemyDistance)
pos = 2;
else {
rand = Random.Range (0, 100);
if(rand%2==0)
pos=1;
else pos=2;
}
can you expound on that? is it throwing errors? not calculating the way you want? in which case how is it calculating and how does that differ to what you want etc.
Taking that snippet in complete vacuum of the rest of your setup, you’re not checking to see if they’re on the same leg (i.e. passed the same number of checkpoints)…
It is not throwing errors but the positions are incorrect( e.g. i get 1/2 even though I am in last place).
Here is a snippet of my checkpoints:
Laps Scripts:
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;
}
}
CheckPoint Script:
using UnityEngine;
using System.Collections;
public class CheckPoint: MonoBehaviour {
[HideInInspector] public static int playerCheckpointsPassed;
public static Transform playerPassed;
void Start(){
playerCheckpointsPassed = 0;
}
void OnTriggerEnter ( Collider other )
{
//Is it the Player who enters the collider?
if (!other.CompareTag("Car1"))
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++;
Debug.Log ("Player:" + Laps.currentCheckpoint);
playerCheckpointsPassed++;
playerPassed = Laps.checkpointA [Laps.currentCheckpoint].transform;
}
else
{
//If we dont have any Checkpoints left, go back to 0
Laps.currentCheckpoint = 0;
}
}
}
}
Enemy Checkpoint:
using UnityEngine;
using System.Collections;
public class EnemyCheckPoint: MonoBehaviour {
[HideInInspector] public static int enemyCheckpointsPassed;
void Start(){
enemyCheckpointsPassed = 0;
}
void OnTriggerEnter ( Collider other )
{
//Is it the Player who enters the collider?
if (!other.CompareTag("Car2"))
return; //If it's not the player dont continue
enemyCheckpointsPassed++;
}
}
Positions Script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Position : MonoBehaviour {
public Transform player, enemy;
float playerDistance, enemyDistance;
int pos, rand;
public Text position;
void Start(){
InvokeRepeating ("Distance", 0.5f, 0.5f);
}
// Update is called once per frame
void Distance() {
if (CheckPoint.playerCheckpointsPassed> EnemyCheckPoint.enemyCheckpointsPassed)
pos = 1;
else if (CheckPoint.playerCheckpointsPassed< EnemyCheckPoint.enemyCheckpointsPassed)
pos = 2;
else {
playerDistance = Vector3.Distance (Laps.checkpointA [Laps.currentCheckpoint].transform.position , player.position);
enemyDistance=Vector3.Distance(Laps.checkpointA [Laps.currentCheckpoint].transform.position , enemy.position);
if (playerDistance < enemyDistance)
pos = 1;
else if(playerDistance > enemyDistance)
pos = 2;
else {
rand = Random.Range (0, 100);
if(rand%2==0)
pos=1;
else pos=2;
}
}
position.text = "Position: " + pos + " / 2";
//Debug.Log ("PlayerDistance:"+playerDistance);
//Debug.Log ("EnemyDistance:" + enemyDistance);
}
}
Some notes: This time, I did not omit anything. This is because I would really like it if someone would copy and paste these scripts and try making changes to the scripts to correct these errors.
@hippocoder and @LeftyRighty from my code above, I figured out that I can accurately calulate the amount of checkpoints passed and use that information to create the correct positions. However, when the player and the ai race car are on the same checkpoint the positions are SOMETIMES messed up. Sometimes they work well. I think, though not sure, that this mess up happens when the cars are making a curve. Is their any advice or help you guys can give me?
@hippocoder and @LeftyRighty , did you try my scripts yet? Did you find any thing wrong with the scripts?
Other than it’s not really what I suggested, no
First off, you should probably be figuring out how to get a number between 0 and 1 based on however many points. So if there’s 10 points and you’re halfway between all those points, you should be 0.5.
If you can manage that, you have won. That’s the main problem facing you… to do this you want to evaluate the position along a path really. Try to envision the problem as a path and finding out where you are along that path.
I am not sure I fully understand. Can you, I don’t know, provide an example or a snippet of code from which I can read?
@hippocoder Anyways, is your method the only way to create race positions in games or can my method work?
really old thread but it could help someone in future searching of good solution… what I propose is to use spline which starts from start wolrd position and ends at the end of track world position. then find most closest position in a spline based on point (it could be a nose of a car) and then sort results of an evaluation from a curve and you will get also good results which could be even smoother then by using checkpoints