c# - 如何在 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;
}
}
这些函数有点工作,但它们基本上每帧都被调用,导致烟雾效果每帧都打开并且有时会闪烁。
解决方案
问题似乎在于您没有在不使用 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);
}
推荐阅读
- azure-data-explorer - 将搜索 * 运算符与“数据库”功能结合使用
- c# - 随机重命名文件方法
- python - 从项目列表中创建特定形状的熊猫数据框
- python - 如何使用 Python 中的 ascii 翻译将十六进制值转换为整数?
- android-studio - 按钮在 Android Studio 3.4 的布局编辑器中不可见
- javascript - 如何使canvas html中的区域可放置?
- regex - 在模式中查找逗号
- java - String.format() 作为常量表达式
- java - 如何限制 Single.Zip 并行性?
- javascript - 通过改变其形式的一键对数组进行排序