一,把陷阱制作成预制体;
二,把角色死亡特效制作成预制体
三,有一些公共变量要拖进脚本里
四,特效要及时的销毁,给特效预制体添加脚本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 () {
}
}