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);
    }

}

 

  

 


免責聲明!

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



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