unity-game-engine - 需要以特定方式旋转物体 - 被万向节锁卡住?

标签 unity-game-engine rotation quaternions euler-angles

我正在开发一个小型迷你游戏,需要根据您滑动的方向将立方体旋转 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

您遇到一个问题,即采用当前欧拉角并尝试添加/减去 90,由于旋转引用系的旋转性质,这不一定与所需位置相关。

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

关于unity-game-engine - 需要以特定方式旋转物体 - 被万向节锁卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50865878/

相关文章:

c# - 使用 JsonFX 反序列化字典

c# - 从 PersistentDataPath 加载图像

css - 如何使用 CSS 居中旋转文本

ios - 为什么 iOS 模拟器在我旋转设备时显示黑屏?

iphone - Madgwick IMU算法在iphone上模拟

python - numpy 中球坐标创建四元数

c# - 处理游戏中的人口(统一)

c# - 如何避免在OnTriggerEnter()中调用GetComponent()?

ios - 旋转后翻译 UIView

c++ - 小四元数相机错误? (在 Z 轴上略微旋转)