unity3D制作暂停游戏和继续游戏12

菜单出来时 枪不能旋转 游戏需要暂停

创建空物体控制所有的游戏状态

创建脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    //1.设置暂停
    public bool isPaused = true;
    public GameObject menuGo;

}

赋值

image

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    //1.设置暂停
    public bool isPaused = true;
    //2.获得组件
    public GameObject menuGo;

    private void Awake()
    {
        Pause();    
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Pause();
        }
    }

    private void Pause()
    {
        isPaused = true;
        menuGo.SetActive(true);
        Time.timeScale = 0;
        //隐藏鼠标图标
        Cursor.visible = true;
    }

    private void UnPause()
    {
        isPaused = false;
        menuGo.SetActive(false);
        Time.timeScale = 1;
        Cursor.visible=false;
    }

    public void ContinueGame()
    {
        UnPause();
    }
        }

赋值按键

image

封装方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager _instance; 
    //1.设置暂停
    public bool isPaused = true;
    //2.获得组件
    public GameObject menuGo;

    private void Awake()
    {
        _instance = this;
        Pause();    
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Pause();
        }
    }

    private void Pause()
    {
        isPaused = true;
        menuGo.SetActive(true);
        Time.timeScale = 0;
        //隐藏鼠标图标
        Cursor.visible = true;
    }

    private void UnPause()
    {
        isPaused = false;
        menuGo.SetActive(false);
        Time.timeScale = 1;
        Cursor.visible=false;
    }

    public void ContinueGame()
    {
        UnPause();
    }
        }

在GunManager中调用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunManager : MonoBehaviour
{
    //1.枪旋转角度 xy轴轩轩角度
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;

    //2.枪射击时间 射击间隔时长
    private float shootTime = 1;
    private float shootTimer = 0;

    //7.得到子弹物体和位置信息
    public GameObject bulletGO;
    public Transform firePosition;

    //3.更新
    private void Update()
    {
        //14.调用GameManager
        if (GameManager._instance.isPaused == false)
        {
            shootTimer += Time.deltaTime;
            if (shootTimer > shootTime)
            {
                //8.点击鼠标左键进行射击
                if (Input.GetMouseButtonDown(0))
                {
                    //9.实例化子弹
                    GameObject bulletCurrent = GameObject.Instantiate(bulletGO, firePosition.position, Quaternion.identity);
                    //11.获得子弹后 通过刚体组件给子弹添加一个正前方向上的力,以达到让子弹向前运动的效果
                    bulletCurrent.GetComponent<Rigidbody>().AddForce(transform.forward * 2400);

                    //12.获得组件后做操作 播放手枪开火动画
                    gameObject.GetComponent<Animation>().Play();
                    //10.计时器归零 每1秒生成一次
                    shootTimer = 0;

                    //13.调用UIManager
                    UIManager._instance.AddShootNum();
                }
            }

            //4.旋转 获取鼠标在屏幕上的位置坐标 去旋转手枪
            float xPosPrecent = Input.mousePosition.x / Screen.width;
            float yPosPrecent = Input.mousePosition.y / Screen.height;

            //5.规定旋转角度最大最小值范围
            float xAngle = -Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation) + 15;
            float yAngle = Mathf.Clamp(xPosPrecent * maxYRotation, minYRotation, maxYRotation) - 60;

            //6.赋值给枪的组件值
            transform.eulerAngles = new Vector3(xAngle, yAngle, 0);
        }
    }
        

}

posted @ 2023-03-05 15:08  flyall  阅读(660)  评论(0)    收藏  举报