C# 将cursor.LockMode更改为Locked会使Input.GetAxis(“鼠标X”)变成一个非常大的数字
在我的游戏中,我有一个随着鼠标旋转的角色。为了防止用户在游戏窗口外点击,我将光标锁定在屏幕中央,以便他们在游戏中使用。当用户暂停游戏时,鼠标将解锁,以便他们可以与暂停菜单按钮交互。当它们恢复时,鼠标锁定回屏幕中心,然而这给了“鼠标X”轴一个巨大的数字,这使得角色“跳跃”到一个我不希望发生的新旋转 以下是基本代码:C# 将cursor.LockMode更改为Locked会使Input.GetAxis(“鼠标X”)变成一个非常大的数字,c#,unity3d,C#,Unity3d,在我的游戏中,我有一个随着鼠标旋转的角色。为了防止用户在游戏窗口外点击,我将光标锁定在屏幕中央,以便他们在游戏中使用。当用户暂停游戏时,鼠标将解锁,以便他们可以与暂停菜单按钮交互。当它们恢复时,鼠标锁定回屏幕中心,然而这给了“鼠标X”轴一个巨大的数字,这使得角色“跳跃”到一个我不希望发生的新旋转 以下是基本代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
bool gamePaused = false;
void Start() {
Cursor.lockState = CursorLockMode.Locked;
}
void Update() {
if (!gamePaused)
CharacterRotation();
}
void CharacterRotation() {
transform.Rotate(0, Input.GetAxis("Mouse X") * 10, 0);
}
public void Pause() {
gamePaused = true;
Cursor.lockState = CursorLockMode.None;
}
public void Resume() {
gamePaused = false;
Cursor.lockState = CursorLockMode.Locked;
}
}
有没有办法防止这种情况?任何帮助都将不胜感激:)
这是因为光标(例如)从屏幕边缘移动到中心,从而产生巨大的数字(screenCenter-screenEdge=>巨大的增量)。为了防止它:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
bool gamePaused = false;
bool afterPause = false;
void Start() {
Cursor.lockState = CursorLockMode.Locked;
}
void Update() {
if (!gamePaused)
CharacterRotation();
}
void CharacterRotation() {
if(afterPause)
{
afterPause = false;
return;
}
transform.Rotate(0, Input.GetAxis("Mouse X") * 10, 0);
}
public void Pause() {
gamePaused = true;
Cursor.lockState = CursorLockMode.None;
}
public void Resume() {
gamePaused = false;
afterPause = true;
Cursor.lockState = CursorLockMode.Locked;
}
}
谢谢你的帮助!不幸的是,我几天前尝试过这个,但跳转仍然发生。你看,当你再次移动光标时,只有一个巨大的三角形。我曾尝试在“if(Input.GetAxis(“Mouse X”)!=0)”语句中使用afterPause if check,但它仍然跳转。目前,我已经解决了这个问题,让暂停菜单由键盘输入而不是鼠标输入控制,这意味着我可以一直锁定光标。无论如何,谢谢你的努力。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
bool gamePaused = false;
bool afterPause = false;
void Start() {
Cursor.lockState = CursorLockMode.Locked;
}
void Update() {
if (!gamePaused)
CharacterRotation();
}
void CharacterRotation() {
if(afterPause)
{
afterPause = false;
return;
}
transform.Rotate(0, Input.GetAxis("Mouse X") * 10, 0);
}
public void Pause() {
gamePaused = true;
Cursor.lockState = CursorLockMode.None;
}
public void Resume() {
gamePaused = false;
afterPause = true;
Cursor.lockState = CursorLockMode.Locked;
}
}