Unity 2D角色復活點與復活等待時間設定
阿新 • • 發佈:2018-12-15
一,新建一個空物體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); } }