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. Dismiss Notice

Making Player respawn after stepping onto tile with collider

Discussion in 'Scripting' started by TomDry82, Jul 26, 2021.

  1. TomDry82

    TomDry82

    Joined:
    Aug 31, 2020
    Posts:
    2
    i have a puzzle room in my game with a maze and i have a set path of tiles with colliders that aren't triggers then i have the rest that are triggers and i want them to trigger the player to respawn at the start of the maze i am a complete noob and dont know how to do this i have tried and failed and all the tutorials i have seen haven't helped.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    What have you tried?
     
  3. TomDry82

    TomDry82

    Joined:
    Aug 31, 2020
    Posts:
    2
    i have tried having a collider on the tile setting it as a trigger and then making it teleport the player which has a collider and the tag Player to a different position but only ends up teleporting the tile it only teleports the player as it looks for the tag Player
     
  4. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    There is a million ways to skin a cat so they say. So we are going to call the triggers that respawn the character kill volumes. I'm assuming your character has a rigidbody since you want it to interact with triggers. If you have gravity enabled, you could get away with a single kill volume for the entire grid by just lowering it a little bit beneath the tiles with colliders. Your kill volume script might look something like this:

    Code (CSharp):
    1. public class KillVolume : MonoBehaviour
    2. {
    3.     [SerializeField] private Transform m_RespawnPosition;
    4.    
    5.     private void OnTriggerEnter(Collider otherCollder)
    6.     {
    7.         var player = otherCollider.GetComponent<Player>();
    8.         if(player != null)
    9.             player.transform.position = m_RespawnPosition.position;
    10.     }
    11. }
    In this example I offer an alternative to using tags. You can still use tags. I set the player's position via it's transform. However, if it's being driven by a rigidbody, you should really set its position via the rigidbody component.