unity--简单用GameManager加载游戏场景与重启游戏

console

有的面板没有找见Console,那我们需要在window里边找到General点击Console就可以在面板上看见。

1.创建一个GameManager

2.在Scripts创建一个组件

GameManager是一个脚本,跟踪游戏和流程,可以改变游戏中的状态

3.将Scripts的GaneManager拖到Herarchy的GameManager中

4.打开cs文件

重启分析

角色死亡条件:碰到障碍物/掉下平台

1、通过碰撞

2.角色的位置y<0;

重启场景。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;//使用场景管理器的时候

public class GameManager : MonoBehaviour
{
    bool gameHasEnded=false;
    public float ResetTime=1f;
    public void EndGame()//角色死亡时候触发游戏结束的这个函数
    {
        
        if (gameHasEnded ==false)
        {
            gameHasEnded= true;
            Debug.Log("Game over");
            Reset();
            Invoke("Reset",ResetTime);
          

        }
    }
    private void Reset()//重启游戏
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);//使用LoadScene来加载场景,激活的场景。
        //SceneManager.LoadScene("TestCube");
    }
}

在player还得去引进GameManager,命名gameManager


using UnityEngine;
using UnityEngine.UI;

public class PlayerMovement : MonoBehaviour
{
    public GameManager gameManager;
    public Rigidbody rb;
    public float forwardForce = 2000f;
    public float sideWaysForce = 500f;
    bool leftPressed;//布尔值判断是否
    bool rightPressed;
    public Text score;//得分

    void Start()
    {
       // rb.AddForce(0, 200, 500);
        //rb.useGravity = false;
    }

    
    void Update()//每帧都运行,输入部分放置在这里
    {
        leftPressed = Input.GetKey("a");
        rightPressed= Input.GetKey("d");
        score.text = transform.position.z.ToString("0");//使得计数只有一位
    }
    private void FixedUpdate()//物理类型
    {
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);//均衡不同电脑帧率,forwardForce控制前进速度
        if (Input.GetKey("d"))
        {
            rb.AddForce(sideWaysForce * Time.deltaTime, 0, 0,ForceMode.VelocityChange);//siderWaysForce控制左右速度,可以在面板上改变。

        }
        if (Input.GetKey("a"))
        {
            rb.AddForce(-sideWaysForce * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
        }
        if(rb.position.y<-1)
        {
            FindObjectOfType<GameManager>().EndGame();
        }
    }
    
   
    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log("We hit Something");
        //判断条件if
        if (collision.gameObject.tag == "Obstacle")
        {
            //停止
            //this.enabled = false;//类让他停下,禁止
            rb.isKinematic = true;//碰到障碍物时候停止,可代表角色的死亡
            //Time.timeScale = 0;//时间停止
           // rb.Sleep();//不可以,使用这个x轴还在动
           FindObjectOfType<GameManager>().EndGame();

        }
    }
}

在player这还需要将Hierarchy的GameManager拖过来

player可以做成预制体。

5.用判读语句显示一个game over

 bool gameHasEnded=false;判断游戏结束

 if (gameHasEnded ==false)
        {
            gameHasEnded= true;
            Debug.Log("Game over");
            Reset();
            Invoke("Reset",ResetTime);
          

        }

6.重启游戏

所有的代码已经粘在上边了。

获取激活场景的名字:

SceneManager.GetActiveScene().name

Invoke();

里边有两个值,string和float的,名字和时间值,在规定时间以后调用传递进来的方法。

Invoke("Reset:,1f);

更精细化是设置一个float值

public float RestTime=1f;

在面板上公开了,可以随时调节时间。在几秒后重新回到游戏开始。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值