需要以特定方式旋转对象 - 卡在 Gimbal Lock 上?

Need to rotate object in specific ways - Stuck with Gimbal Lock?

我正在开发一个小游戏,需要根据您滑动的方向将立方体旋转 90 度。所以你可以向上滑动,它会向上旋转 90 度,然后它们立即向左滑动,它会从你当前的旋转方向向左滑动 90 度(所以它也会保持向上旋转 90 度)。我觉得这应该很简单,但它给我带来了很多麻烦。

我想使用 Lerp/Slerp 这样旋转看起来不错,尽管这不是完全必要的。我目前实现它的方式,例如,每次我调用我的 "SlerpRotateLeft()" 函数时,它每次都只旋转到相对于世界的完全相同的精确旋转(而不是当前旋转 + 90 度在正确的方向).

我整天都在研究四元数和欧拉角,但我仍然不完全确定我的问题是什么。

我目前正在使用状态来确定对象当前旋转的时间和方向,尽管我觉得我可能过于复杂了。此问题的任何可能解决方案(您可以在特定方向上以任何顺序连续滑动,以将立方体沿该特定方向旋转 90 度)。以前,我尝试过使用协程,但它们也没有达到预期的效果(而且我无法重置它们)。

这是我的class。它可以工作,您可以通过将脚本放入编辑器中的任何多维数据集对象来测试它,但它没有按预期工作。您将通过测试了解我的问题所在(我建议在立方体的正面放置一张图像以跟踪它是哪一个)。我不确定我是否正确解释了我的问题,所以如果需要更多信息,请告诉我。

****更新:我接受@Draco18s 的回答是正确的,因为他们的解决方案有效。但是,我没有完全理解解决方案,或者如何存储值。我找到了一个类似问题的答案,该问题也使用了 Transform.Rotate,并存储了该值,这有助于清除解决方案。关键似乎是将它存储在一个游戏对象中,而不是像我最初想象的那样存储在一个四元数中。我想我应该提供这段代码以防万一有人偶然发现这个并且同样感到困惑,尽管你可能不需要滑动检测:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotater : MonoBehaviour
{

 private GameObject endRotation;


//SWIPE VARIABLES
public Vector2 touchStart = new Vector2(0, 0);

public Vector2 touchEnd = new Vector2(0, 0);

public Vector2 currentSwipe = new Vector2(0, 0);

public Vector2 currentSwipeNormal = new Vector2(0, 0);

// Use this for initialization
void Start()
{
    endRotation = new GameObject();

}

// Update is called once per frame
void Update()
{


    if (Input.GetMouseButtonDown(0))
    {
        touchStart = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        //Debug.Log("Touched at: " + touchStart);

    }

    if (Input.GetMouseButtonUp(0))
    {
        touchEnd = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

        //Get Swipe Vector information
        currentSwipe = new Vector2(touchEnd.x - touchStart.x, touchEnd.y - touchStart.y);

        //Normalize Swipe Vector
        currentSwipeNormal = currentSwipe;
        currentSwipeNormal.Normalize();

        //Swipe up
        if (currentSwipeNormal.y > 0 && currentSwipeNormal.x > -0.5 && currentSwipeNormal.x < 0.5)
        {
            endRotation.transform.Rotate(-Vector3.left, 90, Space.World);

        }
        //Swipe down
        if (currentSwipeNormal.y < 0 && currentSwipeNormal.x > -0.5 && currentSwipeNormal.x < 0.5)
        {
            endRotation.transform.Rotate(Vector3.left, 90, Space.World);


        }
        //Swipe left
        if (currentSwipeNormal.x < 0 && currentSwipeNormal.y > -0.5 && currentSwipeNormal.y < 0.5)
        {
            endRotation.transform.Rotate(Vector3.up, 90, Space.World);



        }
        //Swipe right
        if (currentSwipeNormal.x > 0 && currentSwipeNormal.y > -0.5 && currentSwipeNormal.y < 0.5)
        {
            endRotation.transform.Rotate(-Vector3.up, 90, Space.World);

        }

    }

    LerpRotate();
}

void LerpRotate()
{
    transform.rotation = Quaternion.Lerp(transform.rotation, endRotation.transform.rotation, Time.deltaTime * 10);

}
}

使用Transform.RotateAround

您遇到了一个问题,您采用当前的欧拉角并尝试 add/subtract 90,由于旋转参考系的旋转性质,这不一定与所需位置相关。

但是使用 RotateAround,您可以传入全局 UpLeftForward 向量,这正是您想要做的。