Unity 2D地面陷阱和死亡特效


一,把陷阱制作成預制體;

二,把角色死亡特效制作成預制體

三,有一些公共變量要拖進腳本里

四,特效要及時的銷毀,給特效預制體添加腳本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 () {
        
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM