【问题标题】:Unity Clamping Rotation that is controlled by WASD由 WASD 控制的 Unity 夹紧旋转
【发布时间】:2021-11-24 17:05:34
【问题描述】:

我想夹住旋转,旋转是由WASD键控制的。

我该怎么做?我尝试了很多东西,但它不起作用。

    float vertical = Input.GetAxis("Vertical");
    float horizontal = Input.GetAxis("Horizontal");

    transform.Rotate(vertical / 4, horizontal / 4, 0.0f);

【问题讨论】:

  • 你到底做了什么?它产生的结果与您的预期有何不同?

标签: c# unity3d quaternions


【解决方案1】:

您要做的是存储绝对值,例如

// Adjust these via Inspector
public float minPitch = -45;
public float maxPitch = 45;
public float minYaw = -90;
public float maxYaw = 90;
public Vector2 sensitivity = Vector2.one * 0.25f;

private float pitch;
private float yaw;
private Quaternion originalRotation;

private void Awake ()
{
    originalRotation = transform.rotation;
}

void Update ()
{
    pitch += Input.GetAxis("Vertical") * sensitivity.x;
    yaw += Input.GetAxis("Horizontal") * sensitivity.y;

    pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
    yaw = Mathf.Clamp(yaw, minYaw, maxYaw);

    transform.rotation = originalRotation * Quaternion.Euler(pitch, yaw, 0);
}

【讨论】:

  • 谢谢,我明天试试!
猜你喜欢
  • 2020-08-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
相关资源
最近更新 更多