【问题标题】:Unity3D top down plane banking calculationUnity3D 自上而下的平面银行计算
【发布时间】:2021-06-20 09:29:00
【问题描述】:

目前我正在开发一个自上而下的平面游戏,所有运动都是使用 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 来计算点积和叉积,以尝试获得一个用于银行的值,但没有运气。

抱歉,如果有点含糊,我不太清楚我要查找的术语

【问题讨论】:

  • 什么是localRawInput?输入是浮点数吗?
  • localRawInput 是左摇杆的方向,我缓存了它,所以当没有输入时,它保持原来的方向而不是恢复到 0
  • 好的,谢谢。正在寻找答案。

标签: unity3d rotation quaternions dot-product cross-product


【解决方案1】:

我建议不要使用旋转来做所有这些事情,而是使用

float xRot = localRawInput.z * turnAmount
float zRot = localRawInput.x * turnAmount;

transform.rotation = Quaternion.Euler(xRot, 0, 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, 0, zRot);

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

【讨论】:

  • 谢谢,但是我该如何处理银行业务?
  • 什么意思?你的意思是保存输入吗?
  • 不,我的意思是当飞机转弯时,飞机会旋转,所以机翼会下降或上升
  • 你也可以尝试设置 transform.up = Vector3.up
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2023-03-28
  • 2013-12-07
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多