Unity 2D角色复活点与复活等待时间设置
一,新建一个空物体LevelManager用来管理我们的复活,金币等脚本
二,为空物体LevelManager新建一个脚本LevelManager;
三,脚本
把Player和LevelManager拖到指定的公共变量中
1,PlayerController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { private Rigidbody2D m_rg; public float MoveSpeed; public float JumpSpeed; //在角色下添加一个空物体 //设置一个跳跃监测点 public Transform CheckPoint; //设置一个跳跃监测半径 public float CheckRadius; //设置一个跳跃监测层---角色与地面的检测 public LayerMask WhatIsGround; //角色默认是否着地--true public bool isGround; private Animator Anim; //存储复活点的位置信息 public Vector2 RespawnPosition; public LevelManager theLevel; void Start () { m_rg = gameObject.GetComponent<Rigidbody2D>(); Anim = gameObject.GetComponent<Animator>(); //游戏刚开始时,玩家的重生点,就是当前的初始位置点 RespawnPosition = transform.position; theLevel = FindObjectOfType<LevelManager>(); } // Update is called once per frame void Update () { // isGround = Physics2D.OverlapCircle(CheckPoint.position, CheckRadius, WhatIsGround); //m_rg.gameObject.transform.rotation= Quaternion.identity; //------------------Input.GetAxisRaw没有小数值,只有整数,不会产生缓动------------------ //角色水平移动 //按住D键,判断如果大于0,则向右开始移动 if (Input.GetAxisRaw("Horizontal") > 0) { m_rg.velocity = new Vector2(MoveSpeed, m_rg.velocity.y); //设置自身缩放的值 transform.localScale = new Vector2(1f,1f); } //角色水平移动 //按住A键,判断如果小于0,则向左开始移动 else if (Input.GetAxisRaw("Horizontal") < 0) { m_rg.velocity = new Vector2(-MoveSpeed, m_rg.velocity.y); //如果new Vector2(-1f, 1f) x值为负数,则图片进行反转显示 transform.localScale = new Vector2(-1f, 1f); } else //角色水平移动 //松开按键,判断如果等于0,则停止移动 { m_rg.velocity = new Vector2(0, m_rg.velocity.y); } //角色按下空格键实现跳跃 //禁止二连跳 //要先判断角色是否在地面上,在地面上可以跳,不在地面上则不能跳 if (Input.GetButtonDown("Jump")&& isGround) { m_rg.velocity = new Vector2(m_rg.velocity.x,JumpSpeed); } Anim.SetFloat("Speed", m_rg.velocity.x); Anim.SetBool("Grouned", isGround); } private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag=="KillPlane") { //gameObject.SetActive(false); //使当前玩家的位置点为,保存的复活点位置 //transform.position = RespawnPosition; theLevel.Respawn(); } //角色与当前的复活点进行碰撞检测 //把当前角色的位置信息,设置为重生的复活点 if (collision.tag == "CheckPoint") { RespawnPosition = collision.transform.position; } } }
2,LevelManager
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LevelManager : MonoBehaviour { public PlayerController thePlayer; 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); //等待X秒后执行 yield return new WaitForSeconds(3); //把复活碰撞点的位置传给角色,让角色在复活碰撞点复活 thePlayer.transform.position = thePlayer.RespawnPosition; //显示角色 thePlayer.gameObject.SetActive(true); } }