Unity鼠标旋转在卡顿尖峰时出现随机偏移问题求助
Unity鼠标旋转在卡顿尖峰时出现随机偏移问题求助
各位Unity开发者们好,我最近在做一款游戏时遇到了一个诡异的问题:每当游戏出现卡顿尖峰的时候,角色的旋转视角就会莫名其妙跳到某个随机位置,完全不受控制。我翻遍了网上的资料都没找到类似的情况,实在没办法了,来这里请教大家!
先给大家看看我的鼠标视角控制代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class mouselook : MonoBehaviour { [Range(1f,1000f)] public float mouseSensitivity; public Transform playerBody; private float xRoatation = 0f; // Start is called before the first frame update void Start() { Cursor.lockState = CursorLockMode.Locked; xRoatation = 0f; } // Update is called once per frame void FixedUpdate() { float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; xRoatation -= mouseY; xRoatation = Mathf.Clamp(xRoatation, -90f, 90f); transform.localRotation = Quaternion.Euler(xRoatation, 0f, 0f); playerBody.Rotate(Vector3.up * mouseX); } }
我还录制了一段视频展示这个问题的实际表现:卡顿出现的瞬间,角色视角会突然跳转到一个完全随机的角度,完全不符合鼠标的操作逻辑。
目前我已经尝试过的解决方法:
- 将视角更新的方法从
Update切换到FixedUpdate,但问题没有任何改善
有没有大佬遇到过类似的问题,或者能看出我代码里的问题所在?麻烦指点一下!
备注:内容来源于stack exchange,提问作者william redgate
相关产品推荐
相关产品推荐

