对象没有沿正确的方向从Atan2到四元数旋转

问题描述 投票:0回答:1

我正在尝试使用Atan2函数以一定角度计算出的点在z轴上旋转对象。然后,我创建一个四元数以将其输入到对象的旋转中。但是,它不会沿假定正确的方向旋转。如果我绘制了Atan2函数给定的角度,我会看到一个顺时针角度系统,但是如果我绘制了应该是正确的角度以便我的对象以正确的方向渲染的角度,我就会看到一个逆时针系统。解决方案使用Atan2接收的值作为键来创建字典,其值是对象旋转正确方向的角度。但是我仍然不知道发生了什么。我希望有人能帮助我理解它,因为没有比解决方案更糟糕的解决方案了,但是不知道发生了什么。

enter image description here

public class ArrowMovement : MonoBehaviour
{
    public Vector2Variable currentPlayerDirection;
    public Vector3Variable currentPlayerPosition;
    public InputAxis inputAxis;

    private float anglePassed;
    private Dictionary<float, float> realAngles = new Dictionary<float, float>();

    void Awake()
    {
        realAngles.Add(-135, -135);
        realAngles.Add(-90, 180);
        realAngles.Add(-45, 135);
        realAngles.Add(0, 90);
        realAngles.Add(45, 45);
        realAngles.Add(90, 0);
        realAngles.Add(135, -45);
        realAngles.Add(180, -90);
    }

    void Update()
    {
        Vector3 offsetVector = new Vector3(0.2f, 0.05f, 0);
        transform.position = currentPlayerPosition.Value;

        // Rotation
        float angle = Mathf.Atan2(inputAxis.horizontal, inputAxis.verticall) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.Euler(0, 0, realAngles[angle]);
        transform.rotation = rotation;
    }
}
unity3d rotation quaternions atan2
1个回答
0
投票

创建字典不是最好的方法,因为它没有考虑您所设置的角度,因此要从一个图转换为另一个图,我会创建一个如下所示的函数:

float ChangeAngleDiagram(float angle) {
    return -(angle + 90f)+180;
}
© www.soinside.com 2019 - 2024. All rights reserved.