首页 > 解决方案 > 如何在 Unity 中使用力来校正角加速度

问题描述

我正在开发一个需要一些真实物理的太空游戏,比如空间中的轨道和动量守恒,问题是,我无法开发一个代码来识别空间中物体的角加速度并使用动量来纠正它军队。

这是我为稳定旋转而编写的函数:

    void RCS_Stabilize()
    {
        Vector3 localangularvelocity = transform.InverseTransformDirection(rigidBody.angularVelocity);
        Debug.Log(localangularvelocity);

        if (!isRCSinUse)
        {
            if (localangularvelocity.x < RCSthreshole)
            {
                RCS_Pitch(1);
            }
            else if (localangularvelocity.x > -RCSthreshole)
            {
                RCS_Pitch(-1);
        }
              // after that i do the same for y and z

这是 RCS_Pitch 函数:

    void RCS_Pitch(int direction)
    {
        rigidBody.AddRelativeTorque(new Vector3(direction * RCSPower, 0, 0));

        if (direction == 1) // Just show the smoke effect
        {
            RCSeffect[0].startSpeed = 1f;
            RCSeffect[3].startSpeed = 1f;
        }
        else
        {
            RCSeffect[1].startSpeed = 1f;
            RCSeffect[2].startSpeed = 1f;
        }
    }

这些函数有点工作,但它们基本上每帧都被调用,导致烟雾效果每帧都打开并且有时会闪烁。

标签: c#unity3dgame-physics

解决方案


问题似乎在于您没有在不使用 ParticleSystems 时重置它们的 startSpeed。

RCS_Stabilize尝试在函数开头添加以下代码:

foreach (var rcsEffect in RCSEffect) {
  rcsEffect.startSpeed = 0f;
}

或者更好的是,为了让它看起来更自然一点,试着在那里添加一个衰减。使用 RCS 推进器会使粒子速度最大化,并且在不使用推进器时会迅速衰减:

foreach (var rcsEffect in RCSEffect) {
  // adjust the 0.3f lerp factor yourself
  rcsEffect.startSpeed = Mathf.Lerp(rcsEffect.startSpeed, 0f, 0.3f);
}

推荐阅读