Home > Blockchain >  Making checkpoints in unity
Making checkpoints in unity

Time:02-11

I am trying to make a script where the player is repawned after hitting a checkpoint and dying subsequently. However, currently nothing happens when my player collides with an object marked as collision, so I could do with some help as to get the script working.

{
    [SerializeField] private Transform player;
    [SerializeField] private Transform respawn1;
    [SerializeField] private Transform respawn2;
    [SerializeField] private Transform respawn3;
    private int checkpointCount = 1;
   

    private void OnTriggerEnter(Collider other)
    {
        Rigidbody RB = player.GetComponent<Rigidbody>();
        if (other.gameObject.tag == "Respawn")
        {
            BoxCollider BC = other.gameObject.GetComponent<BoxCollider>();
            BC.enabled = false;
            checkpointCount  ;
            
        }
        if (other.gameObject.tag == "Collision")
        {
            if (checkpointCount == 2)
            {
                
                player.transform.position = respawn2.transform.position;

            }
            else if (checkpointCount == 3)
            {
                player.transform.position = respawn3.transform.position;
            }
            else
                
                player.transform.position = respawn1.transform.position;
        }
        

Thanks.

CodePudding user response:

If player actually collides with that object tagged as "collision" (by that I mean that object is not just a trigger). You must handle the collision in OnCollisionEnter(Collision).

CodePudding user response:

Do you have the colliders in the checkpoints as a trigger ? You need to check on trigger so you can use OnTriggerEnter otherwise you use OnCollisionEnter

check the is trigger to make it a trigger

  • Related