角色随机地向上或向下看,动作生硬,角度约为90度
我在努力改进角色的鼠标移动。在第一人称场景中,水平移动看起来很完美,但有时会随机地向上看/向下看的机制失控并自行运作。我用于水平和垂直的函数是相同的,但我不确定是什么原因会导致这种情况。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseMovement : MonoBehaviour
{
public float mouseSensitivity = 200f;
float xRotation = 0f;
float YRotation = 0f;
void Start()
{
//Locking the cursor to the middle of the screen and making it invisible
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if (!InventorySystem.Instance.isOpen ) //remove these) && !CraftingSystem.Instance.isOpen)
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
//control rotation around x axis (Look up and down)
xRotation -= mouseY;
//clamp the rotation so we dont Over-rotate (like in real life)
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
//control rotation around y axis (Look up and down)
YRotation += mouseX;
//applying both rotations
transform.localRotation = Quaternion.Euler(xRotation, YRotation, 0f);
}
}
}
解决方案
没错,似乎移除
Time.deltaTime
才是问题所在。下面的代码效果要好得多,但在较低的
public float mouseSensitivity = 3f;
请在这里查看最终的鼠标代码变更:
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
再次感谢你的帮助!
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。