【发布时间】:2018-11-24 17:26:26
【问题描述】:
我正在开发一个小型迷你游戏,它需要根据您滑动的方向将立方体在适当的方向上旋转 90 度。所以你可以向上滑动,它会向上旋转 90 度,然后它们立即向左滑动,它会从你当前的旋转向左滑动 90 度(所以它也会保持向上旋转 90 度)。我觉得这应该很简单,但这给我带来了很多麻烦。
我想使用 Lerp/Slerp 以使旋转看起来不错,尽管这不是完全必要的。我目前实现它的方式,例如,每次我调用我的“SlerpRotateLeft()”函数时,它每次只旋转到相对于世界完全相同的旋转(而不是当前旋转 + 90 度在正确的方向)。
我整天都在阅读四元数和欧拉角,但我仍然不完全确定我的问题是什么。
我目前正在使用状态来确定对象当前正在旋转的时间和方向,尽管我觉得我可能过于复杂了。此问题的任何可能解决方案(您可以在特定方向上以任何顺序连续滑动,以将立方体在该特定方向上旋转 90 度)。以前,我尝试使用协程,但也没有达到预期的效果(而且我无法重置它们)。
这是我的课。它可以工作,您可以通过将脚本放入编辑器中的任何多维数据集对象来测试它,但它不能按预期工作。通过测试你会看到我的问题是什么(我建议在立方体的正面放置一个图像来跟踪它是哪一个)。我不确定我是否正确解释了我的问题,所以如果需要更多信息,请告诉我。
****更新:我已经接受@Draco18s 的回答是正确的,因为他们的解决方案有效。但是,我并没有完全理解解决方案,或者如何存储值。我找到了一个类似问题的答案,该问题也使用了 Transform.Rotate,并存储了值,这有助于理清解决方案。关键似乎是将它存储在 GameObject 中,而不是像我最初想的那样存储在四元数中。我认为我应该提供此代码,以防有人偶然发现并同样感到困惑,尽管您可能不需要滑动检测:
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);
}
}
【问题讨论】:
标签: unity3d rotation quaternions euler-angles