简体   繁体   English

如何让重生系统在 Unity 3D 中工作?

[英]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.我正在 Unity 中开发一个 3D 游戏,如果你从平台上掉下来就会死。 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.从您提供的代码来看,您实际上根本没有重生播放器,您只是禁用它而从不启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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