简体   繁体   中英

How do I get the respawn system working in Unity 3D?

I'm working on a 3D game in Unity where you die if you fall off the platforms. The aim is to respawn after you fall off a platform. At first it worked, but after I made a completely new movement system with a new player and a new camera it no longer works. I want to make the player respawn after falling off a platform, I used a large cube that serves as a collider detector. After the player comes into contact with it, he must randomly appear on one of the nine platforms. Looks like that still works. The only problem is that the player does not respawn. How can I get this working? I already attached the new script. I would certainly appreciate help!

Script on the collision detector:

public class Respawn : MonoBehaviour
 {
     [SerializeField] private Transform player;
 
     // Spawning
     [SerializeField] private string tagName;
     [SerializeField] private GameObject[] spawnPoints;
     [SerializeField] private GameObject selectedSpawnpoint;
 
     // Sound Effects
     public AudioSource source;
     public AudioClip clip;
 
     void Update()
     {
         if(spawnPoints == null)
         {
             spawnPoints = GameObject.FindGameObjectsWithTag(tag);
         }
 
         int rand = Random.Range(0, 8);
         selectedSpawnpoint = spawnPoints [rand];
     }
 
     void OnTriggerEnter(Collider other)
     {
         player.transform.position = selectedSpawnpoint.transform.position;
 
         source.PlayOneShot(clip);
 
         Debug.Log("AU!");
     }
 }

Script on the player:

public class PlayerLives : MonoBehaviour
 {
     public int extraPlayerHearts = 3;
     
     void OnTriggerEnter(Collider other)
     {
         extraPlayerHearts = extraPlayerHearts - 1;
 
         // The spawnpoints will be destroyed if the player reaches 0 extra hearts
         if(extraPlayerHearts <= 0)
         {
             GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("SpawnPoint");
 
             foreach (GameObject go in foundObjects)
             {
                 Destroy(go);
             }
         }
 
         if(extraPlayerHearts < 0)
         {
             Debug.Log("You Died!");
             GetComponent<PlayerController>().enabled = false;
         }
     }
 }

From the code you have provided, you don't actually respawn the player at all you only disable it and never enable it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM