0

我想在 Z 轴上以特定的角度、速度和方向旋转对象,然后停止。

这是我的代码:

Quaternion targetRotation = Quaternion.AngleAxis(currentRotation.rotateValue, Vector3.forward);
float step = currentRotation.speed;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, step);

有了这个,我可以以所需的速度和角度移动,但方向不正确。我所做的是在它达到 180 后将它移动 180 我将它移动 360,这是一个循环。问题是在 360 度之后不是顺时针移动,而是逆时针移动。不知道这里发生了什么需要绝望的帮助。

提前致谢。

4

1 回答 1

0

如果我正确理解您要做什么,那么也许这样的事情会起作用:

private float anglesToRotate;
private Quaternion targetRotation, initialRotation;
private float elapsedTime, duration, speed;

void Start()
{
    time = 0.0f;
    duration= 0.0f;
}

void Update()
{
    if (time < duration)
    {
        time += Time.deltaTime;
        transform.rotation = Quaternion.Lerp(
                                  initialRotation,
                                  targetRotation,
                                  (speed * time) / duration);
    }
}

public void Rotate(float angles, float speed, float duration)
{
     initialRotation = transform.rotation;
     targetRotation = Quaternion.Euler(
                         transform.rotation.x,
                         transform.rotation.y,
                         transform.rotation.z + anglesToRotate);
     time = 0.0f;
}

笔记:

  • 使用正角度沿一个方向旋转,使用负角度沿相反方向旋转(如果角度为 180,则符号无关紧要)。
  • 使用速度和持续时间的值来使旋转更快或更慢。(我必须添加持续时间才能使其与Quaternion.Lerp)一起使用。
  • 我没有对此进行测试,所以我不确定它是否会按预期工作。
于 2016-05-12T05:23:37.933 回答