Unity3D自顶向下平面银行计算

问题描述

目前我正在开发一款自上而下的平面游戏,所有运动都是使用 X 轴和 Z 轴进行的 2D 运动。我已经根据操纵杆方向计算出旋转,但是,我希望飞机在转弯时绕其 Y 轴旋转,但我找不到最好的方法

    if(movementInput)
    {
        localRawInput = rawLSInput;
        InputDirectionCache = rawLSInput;
    }
    else
    {
        localRawInput = InputDirectionCache;
    }

    targetRotation = Quaternion.LookRotation(localRawInput,Vector3.up);
    currentRotation = Quaternion.Rotatetowards(currentRotation,targetRotation,currentRotationSpeed);


    Vector3 targetRot = targetRotation.eulerAngles;
    Vector3 currentRot = currentRotation.eulerAngles;

    Vector3 currentDir = currentRotation * transform.forward;
    Vector3 targetDir = targetRotation * transform.forward;

这就是我计算所需旋转的方式。应用旋转时,速度仅适用于平面的前方(currentRotation 用于设置旋转) 我正在使用 targetDir 和 currentDir 来计算点积和交叉积,以尝试获得一个用于银行的价值,但没有运气。

对不起,如果它有点含糊,我不太确定我正在寻找的术语

解决方法

我建议使用

,而不是用旋转来做所有这些事情
float xRot = localRawInput.z * turnAmount
float zRot = localRawInput.x * turnAmount;

transform.rotation = Quaternion.Euler(xRot,zRot);

这使得当操纵杆向前倾斜时,平面会绕其 Z 轴旋转,使其向前倾斜。

如果你想让飞机慢慢停下来让它更平稳,你可以使用

// turnSpeed must be between 0 and 1
float xRot = Mathf.Lerp(transform.eulerAngles.x,localRawInput.z * turnAmount,turnSpeed);
float zRot = Mathf.Lerp(transform.eulerAngles.z,localRawInput.x * turnAmount,turnSpeed);

transform.rotation = Quaternion.Euler(xRot,zRot);

这会将平面从当前旋转转到目标旋转,但只是部分旋转,因此随着距离变小,它会变慢。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...