首页 > 其他分享 >unity3D制作暂停游戏和继续游戏12

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

时间:2023-03-05 15:23:50浏览次数:35  
标签:unity3D 12 游戏 void float private using true public

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

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

创建脚本

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

}

标签:unity3D,12,游戏,void,float,private,using,true,public
From: https://www.cnblogs.com/flyall/p/17180314.html

相关文章

  • 斯坦福课程 UE4 C++ ARPG游戏实例教程 01.基础AI与行树
    斯坦福课程UE4C++ARPG游戏实例教程0.绪论前言&摘要本篇文章是基于斯坦福UE4C++课程的学习记录。因为B站用户surkea由于学业原因,暂停了课程笔记的更新,这里狗尾续貂,将......
  • 斯坦福课程 UE4 C++ ARPG游戏实例教程 0.绪论
    斯坦福UE4C++课程学习笔记0.绪论前言UEC++在国内目前还处于比较新的一个领域,网上能找到的教程多为蓝图教程,且质量良莠不齐。终于在B站找到了外网搬运的斯坦福UEC++......
  • 12——Promise
    第十二周Promise初识PromisePromise异步操作的一种解决方案回调函数,异步操作的一种解决方案什么时候使用一般用来解决层层嵌套的回调函数,(回调地狱callbac......
  • unity3D制作统计得分UI10
    创建UItext画布选择2D方便观察设置字体位置和字体大小字体颜色等为UI建立脚本......
  • 洛谷P1213 [USACO1.4][IOI1994]时钟 The Clocks
    这是一个暴力枚举题有两种解决方法,第一种用九重for循环(有点麻烦,尽量别用),第二种简化版(虽然行数少了,但难理解),先来看看 题目!!!题目描述考虑将如此安排在一个 3*3 行......
  • unity3D怪物的死亡与刷新09
    死亡动画播放后,下次射击之前消失MonsterManagerusingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassMonsterManager:Mon......
  • 12月将发布 Linux Mint 18.1″Serena” 公测版
    ​​Linux​​ Mint在月度简报上,LinuxMint负责人ClementLefebvre重申强调了关于即将到来LinuxMint18.1“Serena”操作系统的相关信息。该发行版本目前仍在开发进程中,......
  • 12月将发布 Linux Mint 18.1″Serena” 公测版
    Linux Mint在月度简报上,LinuxMint负责人ClementLefebvre重申强调了关于即将到来LinuxMint18.1“Serena”操作系统的相关信息。该发行版本目前仍在开发进程中,......
  • 127.0.0.1与0.0.0.0的区别
    127.0.0.1是一个环回地址,回环的含义应该是自己给自己发消息。并不表示“本机”。0.0.0.0才是真正表示“本网络中的本机”。在实际应用中,一般我们在服务端绑定端口的时......
  • unity3D设置手枪的动画与子弹的自动销毁07
    取消手枪自动播放动画usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassGunManager:MonoBehaviour{//1.枪旋转......