0

目标:我有一个 3 d 按钮。每次单击它时,我都希望它在 X 轴上旋转 180 度。

说明:一侧写着“Turn 1”,另一侧写着“Turn 2”。所以我希望它在转弯时翻转。

脚本旋转按钮但不旋转 180 度,旋转度数随着每次单击而减小,直到按钮被翻转然后停止工作。

请告诉我我做错了什么,我读过关于vector3、四元数和欧拉角的文章,我尝试了所有我能想到的组合。另一种解决方案是使用一个标志并将按钮转回 180 度,如果它已经被旋转但我真的很想了解为什么这不起作用。

这是我的脚本:

public Quaternion newRotation;
public Quaternion oldRotation;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.transform.gameObject.name == "EndTurnButton")
            {
                oldRotation = hit.transform.rotation;
                newRotation = new Quaternion(oldRotation.x + 180, oldRotation.y, oldRotation.z, 0);
                hit.transform.rotation = Quaternion.Lerp(oldRotation, newRotation, Time.deltaTime * 0.1f);
            }
        }
    }
}
4

1 回答 1

2

问题是您将四元数元素与欧拉角混合在一起。newRotation必须使用Quaternion.Euler进行构造。所以你需要的是:

Vector3 newRotationAngles = oldRotation.eulerAngles;
newRotationAngles.x += 180;
newRotation = Quaternion.Euler (newRotationAngles);
于 2013-09-15T11:29:08.980 回答