I'm developing a space game that need to have some real physics, like orbits and conservation of mommentum in space, the problem is, I can't develope a code that identify the angular acceleration of a body in space and correct it using mommentum forces.
Here is the function that I wrote to try to stabilize the rotation:
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
And this is the RCS_Pitch function:
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;
}
}
These functions kinda work, but they're called basically every frame, causing the smoke effect to be turned on every frame and flickering sometimes.
The problem seems to be in that you don't reset the startSpeed of your ParticleSystems when they are not in use.
Try adding the following code at the beginning of the RCS_Stabilize
function:
foreach (var rcsEffect in RCSEffect) {
rcsEffect.startSpeed = 0f;
}
or better yet, to make it a bit more natural-looking, try adding a decay there. Using an RCS thruster would maximize the particle velocity and it quickly decays when thrusters are not in use:
foreach (var rcsEffect in RCSEffect) {
// adjust the 0.3f lerp factor yourself
rcsEffect.startSpeed = Mathf.Lerp(rcsEffect.startSpeed, 0f, 0.3f);
}