Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Racing Game Checkpoint Spawn

Discussion in 'Scripting' started by JacksonTheXtremeGamer, Jun 26, 2020.

  1. JacksonTheXtremeGamer

    JacksonTheXtremeGamer

    Joined:
    Jun 15, 2019
    Posts:
    108
    Need some help with the checkpoint system. Now the scripts uses an Index, and I want the player to teleport to the last checkpoint it passed by using said index. Here are the 3 scripts that is used for the Start/Finish, the checkpoints, and the player:
    Code (CSharp):
    1. public class LapSystem : MonoBehaviour
    2. {
    3.     public int lapNumber;
    4.     public int CheckpointIndex;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         lapNumber = 1;
    10.         CheckpointIndex = 0;
    11.     }
    12.  
    13.     void OnTriggerEnter(Collider other)
    14.     {
    15.         if (other.gameObject.CompareTag("OutOfBounds"))
    16.         {
    17.            //Insert Respawn Gimmick Here
    18.         }
    19.     }
    20. }
    Code (CSharp):
    1. public class CheckpointSystem : MonoBehaviour
    2. {
    3.     public int Index;
    4.  
    5.     void OnTriggerEnter(Collider other)
    6.     {
    7.         if (other.GetComponent<LapSystem>())
    8.         {
    9.             LapSystem Racer = other.GetComponent<LapSystem>();
    10.  
    11.             if(Racer.CheckpointIndex == Index + 1 || Racer.CheckpointIndex == Index - 1)
    12.             {
    13.                 Racer.CheckpointIndex = Index;
    14.                 Debug.Log(Index);
    15.             }
    16.         }
    17.     }
    18. }
    Code (CSharp):
    1. public class CheckpointHandle : MonoBehaviour
    2. {
    3.     public int CheckpointAmt;
    4.  
    5.     void OnTriggerEnter(Collider other)
    6.     {
    7.         if (other.GetComponent<LapSystem>())
    8.         {
    9.             LapSystem Racer = other.GetComponent<LapSystem>();
    10.  
    11.             if(Racer.CheckpointIndex == CheckpointAmt)
    12.             {
    13.                 Racer.CheckpointIndex = 0;
    14.                 Racer.lapNumber++;
    15.                 Debug.Log(Racer.lapNumber);
    16.             }
    17.         }
    18.     }
    19. }
    Any help is appreciated.
     
  2. UnityFuchs

    UnityFuchs

    Joined:
    Apr 18, 2018
    Posts:
    22
    I would do it like this::
    Checkpoints have a trigger-collider and send its checkpoint-ID. Then, if the car drives through a checkpoint, update its checkpoint. If it respawns, get you checkpoint-id and respawn it there.

    Code is untested, but I think you know what I mean.

    Code (CSharp):
    1. public class CheckpointCode : MonoBehaviour
    2. {
    3.     public int number_of_cars; //Set this value at gamestart
    4.     private int[] checkpoints;
    5.  
    6.     private void Start()
    7.     {
    8.         //With 4 cars: checkpoint = [] [] [] []
    9.         checkpoints = new int[number_of_cars];
    10.     }
    11.  
    12.     //Call from a Collider trigger.
    13.     public void SetCheckpoint(int car_id, int checkpoint_id)
    14.     {
    15.         checkpoints[car_id] = checkpoint_id;
    16.     }
    17. }