【问题标题】:How to achieve very responsive mouse input in Unity3d?如何在 Unity3d 中实现非常灵敏的鼠标输入?
【发布时间】:2018-11-20 08:01:50
【问题描述】:
 private void Update()
 {
     yaw += horizontalSensitivity * Input.GetAxisRaw("Mouse X");
     pitch -= verticalSensitivity * Input.GetAxisRaw("Mouse Y") ;

     if (pitch < -pitchCap) { pitch = -1 * pitchCap; }
     if (pitch > pitchCap) { pitch = pitchCap; }

     transform.rotation = Quaternion.Euler(pitch, yaw, 0.0f);
 }

这是我在项目中用于更新相机旋转的代码。这按预期工作,但我注意到延迟特别小。当我移动鼠标时,我可以感觉相机旋转有点滞后。

在其他更大的游戏中,我可以感觉到我的鼠标移动迅速影响了我的相机旋转——一切都感觉非常灵敏。但是,有了这个设置,我不能说我有同样的感觉。有一些输入延迟。

如何减少这种延迟并生成响应更快的鼠标移动脚本?

【问题讨论】:

  • 您是否尝试使用鼠标存档 FPS 效果?
  • 哦,我忘了说。是的,我是。

标签: c# unity3d input camera lag


【解决方案1】:

您必须平滑鼠标的值。您可以通过将Input.GetAxisRaw 更改为Input.GetAxis 来做到这一点。还可以将其与Time.deltaTime 相乘,以使其在所有平台上保持相同。


如果这不能解决您的问题,那么只需修改 localEulerAngles 角度而不是 transform.rotation,后者是 Quaternion。请参阅下面的示例,该示例还限制了 y 轴旋转,因为您正在制作 FPS 控制器:

public float xMoveThreshold = 1000.0f;
public float yMoveThreshold = 1000.0f;

public float yMaxLimit = 45.0f;
public float yMinLimit = -45.0f;


float yRotCounter = 0.0f;
float xRotCounter = 0.0f;

Transform mainCam;

void Start()
{
    mainCam = Camera.main.transform;
}

// Update is called once per frame
void Update()
{
    xRotCounter += Input.GetAxis("Mouse X") * xMoveThreshold * Time.deltaTime;
    yRotCounter += Input.GetAxis("Mouse Y") * yMoveThreshold * Time.deltaTime;
    yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit);
    //xRotCounter = xRotCounter % 360;//Optional
    mainCam.localEulerAngles = new Vector3(-yRotCounter, xRotCounter, 0);
}

【讨论】:

  • 即使有这些变化,我仍然觉得花招滞后。不过谢谢。 localEulerAngles 与使用 transform.rotation 有何不同?
猜你喜欢
  • 1970-01-01
  • 2021-12-22
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多