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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't set up a checkpoint to change the respawn location

Discussion in 'Scripting' started by JPF55, Jun 7, 2015.

  1. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Hello,
    I've been working on a script to respawn the player if they come in contact with a wall, it works fine but I can't seem to change the spawn location with a checkpoint
    Here is my code so far:

    using UnityEngine;
    using System.Collections;

    public class PlayerSpawnCheckpoint : MonoBehaviour
    {
    public GameObject parent;
    public Transform Spawn;


    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "Wall")
    {
    parent.transform.position = Spawn.position;
    }
    }

    }

    I would like to have a code change the Spawn position to a new location when the player goes through a collider with the tag "Checkpoint" so that the next wall they hit will spawn them at the new point not at the beginning of the level. Any ideas on how to do that?
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Simply set Spawn.position to be whatever the "Checkpoint" object's position is. Easy peasy...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerSpawnCheckpoint : MonoBehaviour
    5. {
    6.     public GameObject parent;
    7.     public Transform Spawn;
    8.    
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.gameObject.tag == "Wall")
    12.         {
    13.             parent.transform.position = Spawn.position;
    14.         }
    15.         if (other.gameObject.tag == "Checkpoint")
    16.         {
    17.             Spawn.position = other.gameObject.transform.position;
    18.         }
    19.     }
    20. }
     
  3. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Thanks, got my spawn and checkpoints working
     
    krougeau likes this.