简体   繁体   English

在Unity中启用暂停菜单时禁用相机移动并解锁光标

[英]Disabling camera movement and unlocking cursor on enabling pause menu in Unity

TLDR: Trying to achieve: TLDR:尝试实现:

  • Enabling pause menu disables camera movement启用暂停菜单会禁用相机移动
  • Enabling pause menu unlocks cursor movement启用暂停菜单可解锁光标移动
  • Vice versa for both in disabling pause menu反之亦然,禁用暂停菜单

Preface: I have zero scripting/coding experience.前言:我的脚本/编码经验为零。

I'm an M.Des art school student trying to make a (first person) virtual art installation for my masters research project given in-person installations are a no-go currently.我是一所M.Des艺术学校的学生,由于目前无法进行个人安装,因此我想为我的硕士研究项目制作一个(第一人称)虚拟艺术安装。 I've managed to build most of the game using tutorials and copy-pasting scripts from the internet, but I've come up against something that I think requires some specific scripting, which I'm unable to do.我已经设法通过使用教程和Internet上的粘贴粘贴脚本来构建游戏的大部分内容,但是我遇到了一些我认为需要一些特定脚本编写的东西,但我做不到。

Basically, I've created a pause menu (using Brackeys' pause menu tutorial) however the camera continues to move even while in the menu.基本上,我已经创建了一个暂停菜单(使用Brackeys的暂停菜单教程),但是即使在菜单中,相机仍会继续移动。 I've found multiple threads and questions covering this but they either assume some level of scripting knowledge or don't seem to apply to my camera movement script - I copy-pasted a "smooth mouse look" script as I wasn't a fan of the robotic camera movement of other scripts.我发现有多个线程和问题可以解决此问题,但是它们要么假设一定程度的脚本知识,要么似乎不应用于我的相机运动脚本-我不是一个狂热者,所以我粘贴了一个“平滑的鼠标外观”脚本其他脚本的机器人摄影机运动。

I'm also not sure how to unlock the cursor for the pause menu.我也不确定如何为暂停菜单解锁光标。 Any help would be very much appreciated, but as I said, please know that my knowledge of scripting is essentially nothing!任何帮助将不胜感激,但是正如我所说,请知道我对脚本的了解本质上是一无所有! I would love to spend the time to learn, but time is something I don't have much of currently with the need to focus on creating the artistic content for the installation.我很想花时间学习,但是时间是我目前没有太多时间,需要集中精力为装置创作艺术作品。

Here are the pause menu and camera movement scripts:以下是暂停菜单和摄像机移动脚本:

Pause menu:暂停菜单:

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

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;
    public GameObject pauseMenuUI;

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Return))
        {


            if (GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

    public void Resume()
    {
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        GameIsPaused = false;
    }
    void Pause()

    {
        pauseMenuUI.SetActive(true);
        Time.timeScale = 0f;
        GameIsPaused = true;
    }
    public void LoadMenu()
    {
        Debug.Log("Loading Game...");
    }
    public void QuitGame()
    {
        Debug.Log("Quitting Game...");
    }
}

Camera movement:相机运动:

using UnityEngine;

[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
public class SimpleSmoothMouseLook : MonoBehaviour
{
    Vector2 _mouseAbsolute;
    Vector2 _smoothMouse;

    public Vector2 clampInDegrees = new Vector2(360, 180);
    public bool lockCursor;
    public Vector2 sensitivity = new Vector2(2, 2);
    public Vector2 smoothing = new Vector2(3, 3);
    public Vector2 targetDirection;
    public Vector2 targetCharacterDirection;

    // Assign this if there's a parent object controlling motion, such as a Character Controller.
    // Yaw rotation will affect this object instead of the camera if set.
    public GameObject characterBody;

    void Start()
    {
        // Set target direction to the camera's initial orientation.
        targetDirection = transform.localRotation.eulerAngles;

        // Set target direction for the character body to its inital state.
        if (characterBody)
            targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    }

    void Update()
    {
        // Ensure the cursor is always locked when set
        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

        // Allow the script to clamp based on a desired target value.
        var targetOrientation = Quaternion.Euler(targetDirection);
        var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);

        // Get raw mouse input for a cleaner reading on more sensitive mice.
        var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        // Scale input against the sensitivity setting and multiply that against the smoothing value.
        mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

        // Interpolate mouse movement over time to apply smoothing delta.
        _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
        _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

        // Find the absolute mouse movement value from point zero.
        _mouseAbsolute += _smoothMouse;

        // Clamp and apply the local x value first, so as not to be affected by world transforms.
        if (clampInDegrees.x < 360)
            _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);

        // Then clamp and apply the global y value.
        if (clampInDegrees.y < 360)
            _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);

        transform.localRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right) * targetOrientation;

        // If there's a character body that acts as a parent to the camera
        if (characterBody)
        {
            var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, Vector3.up);
            characterBody.transform.localRotation = yRotation * targetCharacterOrientation;
        }
        else
        {
            var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
            transform.localRotation *= yRotation;
        }
    }
}

Thanks!谢谢!

TLDR: Trying to achieve: TLDR:尝试实现:

  • Enabling pause menu disables camera movement启用暂停菜单会禁用相机移动
  • Enabling pause menu unlocks cursor movement启用暂停菜单可解锁光标移动
  • Vice versa for both in disabling pause menu反之亦然,禁用暂停菜单

Preface: I have zero scripting/coding experience.前言:我的脚本/编码经验为零。

I'm an M.Des art school student trying to make a (first person) virtual art installation for my masters research project given in-person installations are a no-go currently.我是一所M.Des艺术学校的学生,由于目前无法进行个人安装,因此我想为我的硕士研究项目制作一个(第一人称)虚拟艺术安装。 I've managed to build most of the game using tutorials and copy-pasting scripts from the internet, but I've come up against something that I think requires some specific scripting, which I'm unable to do.我已经设法通过使用教程和Internet上的粘贴粘贴脚本来构建游戏的大部分内容,但是我遇到了一些我认为需要一些特定脚本编写的东西,但我做不到。

Basically, I've created a pause menu (using Brackeys' pause menu tutorial) however the camera continues to move even while in the menu.基本上,我已经创建了一个暂停菜单(使用Brackeys的暂停菜单教程),但是即使在菜单中,相机仍会继续移动。 I've found multiple threads and questions covering this but they either assume some level of scripting knowledge or don't seem to apply to my camera movement script - I copy-pasted a "smooth mouse look" script as I wasn't a fan of the robotic camera movement of other scripts.我发现有多个线程和问题可以解决此问题,但是它们要么假设一定程度的脚本知识,要么似乎不应用于我的相机运动脚本-我不是一个狂热者,所以我粘贴了一个“平滑的鼠标外观”脚本其他脚本的机器人摄影机运动。

I'm also not sure how to unlock the cursor for the pause menu.我也不确定如何为暂停菜单解锁光标。 Any help would be very much appreciated, but as I said, please know that my knowledge of scripting is essentially nothing!任何帮助将不胜感激,但是正如我所说,请知道我对脚本的了解本质上是一无所有! I would love to spend the time to learn, but time is something I don't have much of currently with the need to focus on creating the artistic content for the installation.我很想花时间学习,但是时间是我目前没有太多时间,需要集中精力为装置创作艺术作品。

Here are the pause menu and camera movement scripts:以下是暂停菜单和摄像机移动脚本:

Pause menu:暂停菜单:

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

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;
    public GameObject pauseMenuUI;

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Return))
        {


            if (GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

    public void Resume()
    {
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        GameIsPaused = false;
    }
    void Pause()

    {
        pauseMenuUI.SetActive(true);
        Time.timeScale = 0f;
        GameIsPaused = true;
    }
    public void LoadMenu()
    {
        Debug.Log("Loading Game...");
    }
    public void QuitGame()
    {
        Debug.Log("Quitting Game...");
    }
}

Camera movement:相机运动:

using UnityEngine;

[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
public class SimpleSmoothMouseLook : MonoBehaviour
{
    Vector2 _mouseAbsolute;
    Vector2 _smoothMouse;

    public Vector2 clampInDegrees = new Vector2(360, 180);
    public bool lockCursor;
    public Vector2 sensitivity = new Vector2(2, 2);
    public Vector2 smoothing = new Vector2(3, 3);
    public Vector2 targetDirection;
    public Vector2 targetCharacterDirection;

    // Assign this if there's a parent object controlling motion, such as a Character Controller.
    // Yaw rotation will affect this object instead of the camera if set.
    public GameObject characterBody;

    void Start()
    {
        // Set target direction to the camera's initial orientation.
        targetDirection = transform.localRotation.eulerAngles;

        // Set target direction for the character body to its inital state.
        if (characterBody)
            targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
    }

    void Update()
    {
        // Ensure the cursor is always locked when set
        if (lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

        // Allow the script to clamp based on a desired target value.
        var targetOrientation = Quaternion.Euler(targetDirection);
        var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);

        // Get raw mouse input for a cleaner reading on more sensitive mice.
        var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

        // Scale input against the sensitivity setting and multiply that against the smoothing value.
        mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

        // Interpolate mouse movement over time to apply smoothing delta.
        _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
        _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

        // Find the absolute mouse movement value from point zero.
        _mouseAbsolute += _smoothMouse;

        // Clamp and apply the local x value first, so as not to be affected by world transforms.
        if (clampInDegrees.x < 360)
            _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);

        // Then clamp and apply the global y value.
        if (clampInDegrees.y < 360)
            _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);

        transform.localRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right) * targetOrientation;

        // If there's a character body that acts as a parent to the camera
        if (characterBody)
        {
            var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, Vector3.up);
            characterBody.transform.localRotation = yRotation * targetCharacterOrientation;
        }
        else
        {
            var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
            transform.localRotation *= yRotation;
        }
    }
}

Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM