繁体   English   中英

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

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

我正在 Unity 中开发一个 3D 游戏,如果你从平台上掉下来就会死。 目的是在您从平台上掉下来后重生。 起初它有效,但在我用新播放器和新相机制作了一个全新的运动系统后,它不再有效。 我想让玩家在从平台上掉下来后重生,我使用了一个大立方体作为对撞机探测器。 玩家接触到它后,必须随机出现在九个平台之一。 看起来仍然有效。 唯一的问题是玩家不会重生。 我怎样才能让这个工作? 我已经附上了新脚本。 我当然会很感激帮助!

碰撞检测器上的脚本:

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!");
     }
 }

播放器上的脚本:

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;
         }
     }
 }

从您提供的代码来看,您实际上根本没有重生播放器,您只是禁用它而从不启用它。

暂无
暂无

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

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