I'm trying to make a space shooter game in Unity3D with realistic spaceship physics. I have a problem with correct rotations. When I apply torque in global coords system it will always apply them in the same global planes, even when the ship is eg. rolled to a side - if I now yaw, it will rotate it in the global system instead of yawing the ship in its local coords.
Now, when I use relative torque, I can apply yaw, then roll and it's screwed again because now the torque applied for yaw works in rotated (rolled) plane instead of the old plane.
Is there any way to keep the applied torques in the old planes, even if the ship's local plane has rotated?
Screenshots to explain the relative torque problem:
I start off with the ship aligned with the world axes. I apply relative torque to yaw.
The ship rotates in yaw plane (same as world horizontal plane).
I apply roll. The yaw is supposed to continue rotating the ship in the old plane same as world horizontal plane.
But it does not. Instead, the old yaw torque now continues in the new plane which is the local X-Z plane!
Here is the code I'm using to control the spaceship currently, using the relative torque strategy:
public class PlayerControl : MonoBehaviour
{
private Rigidbody rb;
public ManeuverEngineScript FrontRightManeuverEngine;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Q))
{
//rb.AddTorque(0f, 0f, 10f);
rb.AddRelativeTorque(0f, 0f, 10f);
}
if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("W pressed");
rb.AddRelativeTorque(0f, 0f, -10f);
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Debug.Log("LeftArrow pressed");
//rb.AddTorque(0f, -10f, 0f);
rb.AddRelativeTorque(0f, -10f, 0f);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Debug.Log("RightArrow pressed");
rb.AddRelativeTorque(0f, 10f, 0f);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("DownArrow pressed");
rb.AddRelativeTorque(-10f, 0f, 0f);
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("UpArrow pressed");
rb.AddRelativeTorque(10f, 0f, 0f);
}
}
}
Here's a "stop-motion" explanation. The first set of frames shows how I expect the rotations to happen and the second one shows how it actually takes place in Unity.