Unity 2D地面陷阱和死亡特效
阿新 • • 發佈:2018-12-16
位置 urn n) transform als The instant pda lse
一,把陷阱制作成預制體;
二,把角色死亡特效制作成預制體
三,有一些公共變量要拖進腳本裏
四,特效要及時的銷毀,給特效預制體添加腳本DeadDestroy;
五,腳本
1,LevelManager
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LevelManager : MonoBehaviour { public PlayerController thePlayer; //角色死亡特效 public GameObject DeadExplosion;void Start () { //在遊戲運行時,首先遍歷一遍PlayerController腳本 PlayerController thePlayer = FindObjectOfType<PlayerController>(); } // Update is called once per frame void Update () { } /// <summary> /// 調用方法Respawn開啟攜程方法RespawnCo,完成等待復活事件 /// </summary>public void Respawn() { //開啟攜程RespawnCo StartCoroutine("RespawnCo"); } /// <summary> /// 攜程,等待X秒後復活角色 /// </summary> /// <returns></returns> public IEnumerator RespawnCo() { //隱藏角色 thePlayer.gameObject.SetActive(false); //實例化一個角色死亡特效 Instantiate(DeadExplosion,thePlayer.transform.position,thePlayer.transform.rotation); //等待X秒後執行 yield return new WaitForSeconds(3); //把復活碰撞點的位置傳給角色,讓角色在復活碰撞點復活 thePlayer.transform.position = thePlayer.RespawnPosition; //顯示角色 thePlayer.gameObject.SetActive(true); } }
2,HrutController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class HrutController : MonoBehaviour { //要把LevelManager拖進腳本 public LevelManager theLevel; void Start () { theLevel = FindObjectOfType<LevelManager>(); } // Update is called once per frame void Update () { } //角色碰到陷阱的時候調用Respawn復活角色 private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Player") { theLevel.Respawn(); } } }
3,DeadDestroy
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DeadDestroy : MonoBehaviour { // Use this for initialization void Start () { Destroy(gameObject,1.5f); } // Update is called once per frame void Update () { } }
Unity 2D地面陷阱和死亡特效