Checkpoint system using a list with objects. c# in unity

Hi. So im trying to make a checkpoint system that when you die you respawn at the latest checkpoint. Im using one script that is gonna handle the switching of the one you will respawn at. And another script that handles the respawn at the right checkpoint position.
This is the script that handles the switching of index.

public class CheckPointHandler : MonoBehaviour {
  
    public List<Check> checkPoints = new List<Check> {};
    public int currentCheck;
   

    //public static GameObject[] checkPointsList;

    //public bool active = false;










    // int currentIndex;
    // public List<Check> CheckPoints { get { return CheckPoints; } }
    // public Check curentCheckPoint { get { return CheckPoints[currentIndex]; } }
    // public static CheckPointHandler Instance { get { return Instance; } }

    // List<Check> checkpoints = new List<Check>();
    // int curIndex = 0;
    // static CheckPointHandler instance = null;
    //// protected override void Awake()
    // //{
    //     //instance = this;
    //     //for(int i=0; i <transform.childCount;i++)
    //     //{
    //         //Check checkpoint = transform.GetChild(i).GetComponent<Check>();
    //         //checkpoint.onTrigger += onCheckPointTriggered;
    //         //checkpoints.add(checkpoint);
    //     //}
    //// }
    // //public void onCheckpointTriggered(Check newcheckpoint)
    // //{
    // //    curIndex = chekcpioint.indexOf(newCHeckPoint);
    // //}


    // Use this for initialization
    void Start () {

       
       
    }

    private void OnTriggerEnter(Collider other)
    {
       
      
            if (other.gameObject.tag == "Checkpoint");
            {
                for (int i = 0; i < 5; i++)
                {
                    print("Hel Aliens");
                    print(checkPoints[i]);
                 }
                   
               
            }


    }

Do you have a question?

1 Like

ops missed that i dident ask a question. But i was wondering if im on the right path on the script above or if im way of?
And the script above is the one who handles the switching of which the current checkpoint is and i have Another who handles the positioning of the player.

this is the function who handles position

public static Vector3 GetAvtiveCheckPointPosition()
{
Vector3 result = new Vector3(-1898.2f, -3f,-15f);
if(checkPointsList!=null)
{
print(“avtive”);
foreach(GameObject cp in checkPointsList)
{
if(cp.GetComponent().activated)
{
result = cp.transform.position;
break;
}

}
}

return result;

}

The approach I personally use is to have a script called CheckpointController which simply has an integer property on it named “CheckpointIndex”. I put this script on gameObjects with a Trigger collider on them. In OnTriggerEnter, I check whether a static “LastCheckpointIndex” value is less than the current CheckpointController’s index. If so, I increment “LastCheckpointIndex”.

When respawning, I do something like:

var lastCheckpoint = GameObject.FindObjectsOfType<CheckpointController>().FirstOrDefault(c => c.CheckpointIndex == LastCheckpointIndex);

Then I can use the objects’s position and rotation as my player’s spawn position and rotation. It’s pretty simple, assuming your checkpoints have a meaningful ascending order.

1 Like