Unity中如何慢慢地向某个角度旋转?
0 1674
0

我正在使用SteamVR,并想根据控制器已升高的高度旋转摄像机,所以我编写了将控制器的高度发送到摄像机脚本方法。

当我执行以下操作时,轮换工作: transform.Rotate(0, 0, 30); // 30 is just to test, we will use the controller height later

但是我想慢慢地进行此轮换,而不是立即发生。 我也尝试过使用Quaternion.Lerp,但是它似乎没有用。 Quaternion.Lerp(this.transform.rotation, new Quaternion(this.transform.rotation.eulerAngles.x, this.transform.rotation.eulerAngles.y, zPosition, 0), Time.deltaTime * 5);

有什么建议?

收藏
2021-01-12 11:52 更新 gitvrar •  463
共 1 个回答
高赞 时间
0

Quaternion.Lerp()应该可以很好地满足您的需求。但是,对它的使用为该方法提供了一些不正确的参数,这就是为什么它无法按预期工作的原因。(此外,您可能不应该使用其构造函数来构造四元数,有一些辅助方法可以正确地从欧拉角进行转换。)

您需要做的是记录初始的开始和结束旋转,并在Quaternion.Lerp()每次调用时将它们传递给它。

请注意,您不能仅参考transform.rotation每一帧,因为它会随着对象旋转而变化。例如,调整代码以使其工作:

Quaternion startRotation;
Quaternion endRotation;float rotationProgress = -1;
// Call this to start the rotationvoid StartRotating(float zPosition){

    // Here we cache the starting and target rotations
    startRotation = transform.rotation;
    endRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, zPosition);

    // This starts the rotation, but you can use a boolean flag if it's clearer for you
    rotationProgress = 0;
}
void Update() {
    if (rotationProgress < 1 && rotationProgress >= 0){
        rotationProgress += Time.deltaTime * 5;

        // Here we assign the interpolated rotation to transform.rotation
        // It will range from startRotation (rotationProgress == 0) to endRotation (rotationProgress >= 1)
        transform.rotation = Quaternion.Lerp(startRotation, endRotation, rotationProgress);
    }
}

收藏
2021-01-12 11:55 更新 同步 •  1696