Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help: teleporting the gameobject holding the script

Discussion in 'Scripting' started by cate5171, Sep 19, 2020.

  1. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    Hey everyone!
    I am working on this 3D Game and i am making a respawn thing where a collider is underneath the map and when the player touches it, the player would go to the respawn point. I have one problem though, i want to have multiple objects teleport too. Like if another thing with the script touches it, it won't teleport the both only the one. Kinda like Destroy(gameObject); but with the teleport thing. Any help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DeathDetector : MonoBehaviour {
    6.  
    7.     public GameObject Player;
    8.    private GameObject Respawn;
    9.  
    10.   void OnTriggerEnter(Collider col) {
    11.  
    12. Respawn = GameObject.FindWithTag("Respawn1");
    13.  
    14.    Debug.Log("COllided");
    15.  
    16.           if(col.gameObject.tag == "DeathDetector1")
    17.   {
    18.   Debug.Log("Collision1");
    19. Player.transform.position = Respawn.transform.position;
    20.    Debug.Log("Teleported");
    21.  
    22.     }
    23.   }
    24. }
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Add a component to every game object that should have this behavior. Make it reference it respawns point (or, even better, just a Vector3 for coordinates) and when it touches the collider, place it there.

    For example, your line 19 would look something like:
    col.gameObject.transform.position = col.gameObject.GetComponent<RespawnInfo>().RespawnLocation;