Search Unity

How to create race positions in games?

Discussion in 'Scripting' started by greatness, Aug 12, 2016.

  1. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
    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?
     
  2. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
    :(@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:

    Code (CSharp):
    1. playerDistance = Vector3.Distance (Laps.checkpointA [Laps.currentCheckpoint].transform.position , player.position);
    2.             enemyDistance=Vector3.Distance(Laps.checkpointA [Laps.currentCheckpoint].transform.position , enemy.position);
    3.             if (playerDistance < enemyDistance)
    4.                 pos = 1;
    5.             else if(playerDistance > enemyDistance)
    6.                 pos = 2;
    7.             else {
    8.                 rand = Random.Range (0, 100);
    9.                 if(rand%2==0)
    10.                     pos=1;
    11.                 else pos=2;
    12.         }
     
    unity_4IfZ0mZ9q-zjLw likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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)...
     
  7. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
    :( 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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Laps : MonoBehaviour {
    5.  
    6.     // These Static Variables are accessed in "checkpoint" Script
    7.     public Transform[] checkPointArray;
    8.     public static Transform[] checkpointA;
    9.     public static int currentCheckpoint = 0;
    10.     public static int currentLap = 0;
    11.     public Vector3 startPos;
    12.     public int Lap;
    13.  
    14.     void  Start ()
    15.     {
    16.         startPos = transform.position;
    17.         currentCheckpoint = 0;
    18.         currentLap = 0;
    19.  
    20.     }
    21.  
    22.     void  Update ()
    23.     {
    24.         Lap = currentLap;
    25.         checkpointA = checkPointArray;
    26.     }
    27.  
    28. }
    29.  
    CheckPoint Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CheckPoint: MonoBehaviour {
    5.  
    6.     [HideInInspector] public static int playerCheckpointsPassed;
    7.     public static Transform playerPassed;
    8.  
    9.  
    10.     void Start(){
    11.         playerCheckpointsPassed = 0;
    12.  
    13.     }
    14.  
    15.     void  OnTriggerEnter ( Collider other  )
    16.     {
    17.  
    18.         //Is it the Player who enters the collider?
    19.         if (!other.CompareTag("Car1"))
    20.             return; //If it's not the player dont continue
    21.  
    22.  
    23.  
    24.  
    25.  
    26.         if (transform == Laps.checkpointA[Laps.currentCheckpoint].transform)
    27.         {
    28.             //Check so we dont exceed our checkpoint quantity
    29.             if (Laps.currentCheckpoint + 1 < Laps.checkpointA.Length) {
    30.  
    31.                 //Add to currentLap if currentCheckpoint is 0
    32.                 if (Laps.currentCheckpoint == 0) {
    33.                     Laps.currentLap++;
    34.                 }
    35.  
    36.                 Laps.currentCheckpoint++;
    37.                 Debug.Log ("Player:" + Laps.currentCheckpoint);
    38.                 playerCheckpointsPassed++;
    39.                 playerPassed = Laps.checkpointA [Laps.currentCheckpoint].transform;
    40.  
    41.             }
    42.  
    43.             else
    44.             {
    45.                 //If we dont have any Checkpoints left, go back to 0
    46.                 Laps.currentCheckpoint = 0;
    47.  
    48.             }
    49.         }
    50.      
    51.  
    52.  
    53.  
    54.     }
    55.  
    56.  
    57.  
    58.  
    59. }
    Enemy Checkpoint:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyCheckPoint: MonoBehaviour {
    5.  
    6.     [HideInInspector] public static int enemyCheckpointsPassed;
    7.  
    8.     void Start(){
    9.         enemyCheckpointsPassed = 0;
    10.  
    11.     }
    12.  
    13.     void  OnTriggerEnter ( Collider other  )
    14.     {
    15.  
    16.         //Is it the Player who enters the collider?
    17.         if (!other.CompareTag("Car2"))
    18.             return; //If it's not the player dont continue
    19.  
    20.         enemyCheckpointsPassed++;
    21.  
    22.  
    23.    
    24.         }
    25.  
    26.  
    27.  
    28.     }
    29.    
    30.  
    31.  
    32.  
    33.  
    34.  
    Positions Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Position : MonoBehaviour {
    6.  
    7.     public Transform player, enemy;
    8.     float playerDistance, enemyDistance;
    9.     int pos, rand;
    10.     public Text position;
    11.  
    12.     void Start(){
    13.         InvokeRepeating ("Distance", 0.5f, 0.5f);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Distance() {
    18.  
    19.  
    20.         if (CheckPoint.playerCheckpointsPassed> EnemyCheckPoint.enemyCheckpointsPassed)
    21.             pos = 1;
    22.         else if (CheckPoint.playerCheckpointsPassed< EnemyCheckPoint.enemyCheckpointsPassed)
    23.             pos = 2;
    24.  
    25.         else {
    26.             playerDistance = Vector3.Distance (Laps.checkpointA [Laps.currentCheckpoint].transform.position , player.position);
    27.             enemyDistance=Vector3.Distance(Laps.checkpointA [Laps.currentCheckpoint].transform.position , enemy.position);
    28.             if (playerDistance < enemyDistance)
    29.                 pos = 1;
    30.             else if(playerDistance > enemyDistance)
    31.                 pos = 2;
    32.             else {
    33.                 rand = Random.Range (0, 100);
    34.                 if(rand%2==0)
    35.                     pos=1;
    36.                 else pos=2;
    37.         }
    38.  
    39.     }
    40.  
    41.         position.text = "Position: " + pos + " / 2";
    42.         //Debug.Log ("PlayerDistance:"+playerDistance);
    43.         //Debug.Log ("EnemyDistance:" + enemyDistance);
    44. }
    45. }
    46.  

    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.
     
  8. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
    @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?
     
  9. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  11. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
    o_O 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?
     
  12. greatness

    greatness

    Joined:
    Feb 20, 2016
    Posts:
    199
    @hippocoder Anyways, is your method the only way to create race positions in games or can my method work?
     
  13. FrostweepGames

    FrostweepGames

    Joined:
    Jan 2, 2015
    Posts:
    264
    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