【问题标题】:Unity, rotate an object in axis and back to starting point in the same directionunity,沿轴旋转对象并沿同一方向返回起点
【发布时间】:2018-05-12 22:08:02
【问题描述】:

我想在 Y 方向上匀速旋转一个物体。停止时,我想以相同的方向旋转回 Quaternion.identity。

public bool spin;
public float speed;

private void Update() {
    if (spin) {
        transform.Rotate (-Vector3.up, Time.deltaTime * speed, Space.World);
    } else if (transform.rotaion != Quaternion.identity) {
        transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.identity, Time.deltaTime * speed);
    }
}

这很好用,但它会朝相反的方向旋转。你如何强制它继续向 Quaternion.identity 的原始方向旋转?

【问题讨论】:

    标签: c# unity3d rotation quaternions


    【解决方案1】:

    这很好用,但它会朝相反的方向旋转。

    RotateTowards 将采用最短路径到达其目标 - 无论是哪个方向。

    你如何强制它继续向 Quaternion.identity 的原始方向旋转?

    旋转经过 180 度(或一半)时反转方向:

    var dir = transform.eulerAngles.y < 180f ? 1f : -1f;
    Quaternion.RotateTowards(rotation, Quaternion.identity, Time.deltaTime * speed * dir);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      相关资源
      最近更新 更多