[英]Disabling camera movement and unlocking cursor on enabling pause menu in Unity
TLDR:尝试实现:
前言:我的脚本/编码经验为零。
我是一所M.Des艺术学校的学生,由于目前无法进行个人安装,因此我想为我的硕士研究项目制作一个(第一人称)虚拟艺术安装。 我已经设法通过使用教程和Internet上的粘贴粘贴脚本来构建游戏的大部分内容,但是我遇到了一些我认为需要一些特定脚本编写的东西,但我做不到。
基本上,我已经创建了一个暂停菜单(使用Brackeys的暂停菜单教程),但是即使在菜单中,相机仍会继续移动。 我发现有多个线程和问题可以解决此问题,但是它们要么假设一定程度的脚本知识,要么似乎不应用于我的相机运动脚本-我不是一个狂热者,所以我粘贴了一个“平滑的鼠标外观”脚本其他脚本的机器人摄影机运动。
我也不确定如何为暂停菜单解锁光标。 任何帮助将不胜感激,但是正如我所说,请知道我对脚本的了解本质上是一无所有! 我很想花时间学习,但是时间是我目前没有太多时间,需要集中精力为装置创作艺术作品。
以下是暂停菜单和摄像机移动脚本:
暂停菜单:
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...");
}
}
相机运动:
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;
}
}
}
谢谢!
TLDR:尝试实现:
前言:我的脚本/编码经验为零。
我是一所M.Des艺术学校的学生,由于目前无法进行个人安装,因此我想为我的硕士研究项目制作一个(第一人称)虚拟艺术安装。 我已经设法通过使用教程和Internet上的粘贴粘贴脚本来构建游戏的大部分内容,但是我遇到了一些我认为需要一些特定脚本编写的东西,但我做不到。
基本上,我已经创建了一个暂停菜单(使用Brackeys的暂停菜单教程),但是即使在菜单中,相机仍会继续移动。 我发现有多个线程和问题可以解决此问题,但是它们要么假设一定程度的脚本知识,要么似乎不应用于我的相机运动脚本-我不是一个狂热者,所以我粘贴了一个“平滑的鼠标外观”脚本其他脚本的机器人摄影机运动。
我也不确定如何为暂停菜单解锁光标。 任何帮助将不胜感激,但是正如我所说,请知道我对脚本的了解本质上是一无所有! 我很想花时间学习,但是时间是我目前没有太多时间,需要集中精力为装置创作艺术作品。
以下是暂停菜单和摄像机移动脚本:
暂停菜单:
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...");
}
}
相机运动:
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;
}
}
}
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.