一,把陷阱制作成预制体;
二,把角色死亡特效制作成预制体
三,有一些公共变量要拖进脚本里
四,特效要及时的销毁,给特效预制体添加脚本DeadDestroy;
五,脚本
11,LevelManager 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6 7public class LevelManager : MonoBehaviour { 8 9 public PlayerController thePlayer; 10 11 //角色死亡特效 12 public GameObject DeadExplosion; 13 14 void Start () { 15 16 //在游戏运行时,首先遍历一遍PlayerController脚本 17 PlayerController thePlayer = FindObjectOfType<PlayerController>(); 18 19 } 20 21 // Update is called once per frame 22 void Update () { 23 24 } 25 26 /// <summary> 27 /// 调用方法Respawn开启携程方法RespawnCo,完成等待复活事件 28 /// </summary> 29 public void Respawn() 30 { 31 //开启携程RespawnCo 32 StartCoroutine("RespawnCo"); 33 34 } 35 36 37 /// <summary> 38 /// 携程,等待X秒后复活角色 39 /// </summary> 40 /// <returns></returns> 41 public IEnumerator RespawnCo() 42 { 43 44 //隐藏角色 45 thePlayer.gameObject.SetActive(false); 46 //实例化一个角色死亡特效 47 Instantiate(DeadExplosion,thePlayer.transform.position,thePlayer.transform.rotation); 48 49 50 //等待X秒后执行 51 yield return new WaitForSeconds(3); 52 //把复活碰撞点的位置传给角色,让角色在复活碰撞点复活 53 54 thePlayer.transform.position = thePlayer.RespawnPosition; 55 //显示角色 56 57 thePlayer.gameObject.SetActive(true); 58 } 59 60} 61 622,HrutController 63 64using System.Collections; 65using System.Collections.Generic; 66using UnityEngine; 67 68public class HrutController : MonoBehaviour { 69 70 //要把LevelManager拖进脚本 71 public LevelManager theLevel; 72 73 void Start () { 74 75 theLevel = FindObjectOfType<LevelManager>(); 76 77 } 78 79 // Update is called once per frame 80 void Update () { 81 82 } 83 84 //角色碰到陷阱的时候调用Respawn复活角色 85 private void OnTriggerEnter2D(Collider2D collision) 86 { 87 if (collision.tag == "Player") 88 { 89 theLevel.Respawn(); 90 } 91 } 92} 93 943,DeadDestroy 95 96using System.Collections; 97using System.Collections.Generic; 98using UnityEngine; 99 100public class DeadDestroy : MonoBehaviour { 101 102 // Use this for initialization 103 void Start () { 104 Destroy(gameObject,1.5f); 105 } 106 107 // Update is called once per frame 108 void Update () { 109 110 } 111}