【问题标题】:Mouse movement lagging鼠标移动滞后
【发布时间】:2021-05-19 07:50:36
【问题描述】:

所以我编写了 c# 代码来在第一人称游戏中移动鼠标和鼠标灵敏度,有些地方只是加速并开始滞后 这是代码

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

public class cameramovescript : MonoBehaviour
{

    public float mouseSensitivity = 100f;
    public Transform playerBody;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    删除 Time.deltaTime。这是因为鼠标输入的 Input.GetAxis 为您提供了自上一帧以来鼠标移动的像素。这意味着在较长的帧上,您从 Input.GetAxis 获得的鼠标输入值会更高,而在较慢的帧上,输入会更慢。所以你要做的就是解决这个问题:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class cameramovescript : MonoBehaviour
    {
    
        public float mouseSensitivity = 100f;
        public Transform playerBody;
    
        float xRotation = 0f;
    
        // Start is called before the first frame update
        void Start()
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
    
        // Update is called once per frame
        void Update()
        {
            float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
            float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    
            xRotation -= mouseY;
            xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    
            transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
            playerBody.Rotate(Vector3.up * mouseX);
        }
    }
    

    这是我喜欢在我的项目中使用的功能:

        float GetMouseOrStickLookAxis(string mouseInputName, string stickInputName)
        {
            if (CanProcessInput())
            {
                // Check if this look input is coming from the mouse
                bool isGamepad = Input.GetAxis(stickInputName) != 0f;
                float i = isGamepad ? Input.GetAxis(stickInputName) : Input.GetAxisRaw(mouseInputName);
    
                // handle inverting vertical input
                if (invertYAxis)
                    i *= -1f;
    
                // apply sensitivity multiplier
                i *= lookSensitivity;
    
                if (isGamepad)
                {
                    // since mouse input is already deltaTime-dependant, only scale input with frame time if it's coming from sticks
                    i *= Time.deltaTime;
                }
                else
                {
                    // reduce mouse input amount to be equivalent to stick movement
                    i *= 0.01f;
    #if UNITY_WEBGL
                    // Mouse tends to be even more sensitive in WebGL due to mouse acceleration, so reduce it even more
                    i *= webglLookSensitivityMultiplier;
    #endif
                }
    
                return i;
            }
    
            return 0f;
        }
    

    如果您需要更深入的答案,请查看这篇文章:Mouse movement with deltaTime

    祝你有美好的一天!

    【讨论】:

    • 谢谢你,我的朋友,你真的帮助了我,让我度过了愉快的一天。
    猜你喜欢
    • 2021-11-11
    • 2021-08-12
    • 2021-10-02
    • 2015-12-09
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多