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 () {
        
    }
}

 

转载于:https://www.cnblogs.com/yueqingli/p/10125300.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值