Unity3d获取鼠标输入轴

Unity3d get Mouse Input Axis

我想通过鼠标移动在-20°和+20°之间旋转一个GameObject。

我知道有可能通过 Input.GetAxis("Horizontal");Input.GetAxis("Vertical"); 其中 returns 在 -11

之间浮动值

如果有类似的东西就好了 Input.GetMouseAxis("Horizontal");Input.GetMouseAxis("Horizontal");.

但我的输出应该不受键盘的影响。 很抱歉,我对 Unity 及其 API.

没有太多经验

is the mouse in unity "captured" in screen resolution or how should I scale the mouse Position to a value between -1 and 1 ?

在这种情况下,"normalizedSpeed" 将是您的目标

public float mouseDistance;
public Vector2 mouseDown = -Vector2.one;

public float normalizedSpeed;

public void Update()
{
   if(Input.GetMouseButtonDown(0))
      mouseDown = Input.MousePosition();

   if(Input.GetMouseButtonUp(0))
      mouseDown = -Vector2.one;

   if(Input.GetMouseButton(0))
      normalizedSpeed = mouseDown != -Vector2.one ?
      Mathf.Clamp((mouseDown - Input.MousePosition()).sqrMagnitude, 0, (mouseDistance * mouseDistance)) / (mouseDistance * mouseDistance)
      : 0;
}

我使用 sqrMagnitude 而不是 distance 因为 distance() 中的 sqr root 调用占用大量内存,所以比较平方距离比较快

另外请记住,我将其作为伪代码编写,因此总体思路就是我要实现的目标,我让您在语法上负责实现 ;)