【问题标题】:Unity, custom camera controlled mouse movement script has intense driftUnity,自定义相机控制的鼠标移动脚本有强烈的漂移
【发布时间】:2019-09-30 05:46:22
【问题描述】:

没用

using System.Runtime.InteropServices; //for mouse reset
int mouseXPos = 960; //half of 1920
int mouseYPos = 540; //half of 1080
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
SetCursorPos(mouseXPos, mouseYPos);//Call this when you want to set the mouse position

在固定更新中:

mousePos = Input.mousePosition;
mousePos.z = turnSpeed;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
transform.LookAt(mousePos);

mousePos.z 是相机所看到的目标距离相机多远。 鼠标应该放在屏幕中间,但是当程序构建并在编辑器中时(我必须偏移 mouseXPos 和 mouseYPos 才能使用全屏播放窗口),鼠标不会设置为非常中心,所以有漂移,视图慢慢向上移动。 它似乎偏离了 2.3 像素,当我大幅度移动鼠标时,它不会回到零,而是偏离了 0.1 或 0.2 像素

示例视频: https://imgur.com/a/hPFkGjE 在日志中您可以看到 vector3 应该是 0,0,5。我禁用了视频的旋转。

【问题讨论】:

    标签: visual-studio unity3d camera


    【解决方案1】:

    您真的不应该使用系统库来控制用户输入,尤其是在 Unity 中。使用Cursor.lockStateCursor.visible 属性来控制光标。特别是,您想要做的似乎是将光标锁定到屏幕中心。这可以通过以下方式完成:

    /**
     * Locks the cursor to the centre of the game window.
     */
    private void LockCursor()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }
    
    // ...
    // In some MonoBehaviour...
    private void Start()
    {
        LockCursor();
    }
    

    请参阅documentation 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多