Howdy,
It’s been a while with work and all kinds of fun stuff, time to get back to coding. So, to start I decided I’d work on revamping my space flight system and to support that I’ve started writing the Flight Assist system. Basically the idea is that this guy kicks in and helps fly the ship. So far I’ve got it working on two axes so long as you stay on those axes.
I originally started with the normalized angular velocities on each axis. That didn’t exactly work, so I then broke down those velocities into their x, y, and z components and normalized them myself (turns out the default maximum angular velocity is 7…easy enough). This produced the results I wanted. You pitch or yaw and keep the ship on axis and once you let off the thruster switch, the opposite side thrusters fire to slow you down…I’ve even got it to where it will 0 the velocities below a certain threshold…(it’s a bit of a cheat I know).
Now here’s where it gets fun:
Whenever you maneuver the ship off the x or y axes, the system falls apart. I know it’s working, and it does it’s best to null the velocities but ultimately fails and freaks out. It actually makes things worse and usually results in complete loss of control of the ship.
And that’s where I need help. Sorry for the state of the code, it’s a prototype so it’s not as polished as it could be.
Thoguht…I think maybe the problem lies in an inaccurate velocity read…are the angular velocities using global or local axes? I suspect global, because in my test scene I origiented the ship (see picture) and then turned off the flight assist and thrusted…my Y axis velocity hit the max of 7, but the output is 0.6 and it should be 1…that being said, how would I change that to local? I think if I can solve that this will essentially solve itself…anyway, code and pics!
Flight Test
using UnityEngine;
using System.Collections;
public class FlightTest : MonoBehaviour
{
private GameObject ParentObject = null;
private Rigidbody RB = null;
private GameObject KeyboardObject = null;
private Keyboard KB = null;
public GameObject maneuverJetRootObject;
private ManeuverJetRoot maneuverJetRoot;
private GameObject[] maneuverJets;
private Thruster[] thrusters;
private float normalizedAngularVelocityX, normalizedAngularVelocityY, normalizedAngularVelocityZ;
void Start()
{
ParentObject = gameObject.transform.root.gameObject;
RB = ParentObject.GetComponent<Rigidbody>();
KeyboardObject = gameObject.transform.root.gameObject;
KB = KeyboardObject.GetComponent<Keyboard>();
//Assign the manuevering jet root
maneuverJetRoot = maneuverJetRootObject.GetComponent<ManeuverJetRoot>();
//Next, copy all of the maneuvering jets from the root
maneuverJets = maneuverJetRoot.GetManeuveringJets();
//Initialize the thruster array
thrusters = new Thruster[maneuverJets.Length];
//Copy the thrusters from the root
for (int i = 0; i < maneuverJets.Length; i++)
{
thrusters[i] = maneuverJets[i].GetComponent<Thruster>();
}
}
void Update()
{
normalizedAngularVelocityX = RB.angularVelocity.x / 7.0f;
normalizedAngularVelocityY = RB.angularVelocity.y / 7.0f;
normalizedAngularVelocityZ = RB.angularVelocity.z / 7.0f;
Debug.Log("x: " + normalizedAngularVelocityX + " y: " + normalizedAngularVelocityY + " z: " + normalizedAngularVelocityZ);
//Yaw Control
YawControl();
NullYRotation();
//Pitch Control
PitchControl();
NullXRotation();
}
private void NullXRotation()
{
Debug.Log("X Vel: " + normalizedAngularVelocityX);
//Stop Pitch Down by activating the thrusters on the opposite side of the ship...
if (normalizedAngularVelocityX > 0.0f && KB.WKeyState() != true)
{
Debug.Log("Stop Pitching Down!");
if (normalizedAngularVelocityX >= 0.0f)
{
thrusters[6].ApplyThrust();
thrusters[7].ApplyThrust();
thrusters[8].ApplyThrust();
thrusters[9].ApplyThrust();
}
if (normalizedAngularVelocityX <= 0.0001f)
{
RB.angularVelocity = new Vector3(0.0f, normalizedAngularVelocityY, normalizedAngularVelocityZ);
}
}
//Stop Pitch Up by activating the thrusters on the opposite side of the ship...
if (normalizedAngularVelocityX < 0.0f && KB.SKeyState() != true)
{
Debug.Log("Stop Pitching Up!");
if (normalizedAngularVelocityX <= 0.0f)
{
thrusters[4].ApplyThrust();
thrusters[5].ApplyThrust();
thrusters[10].ApplyThrust();
thrusters[11].ApplyThrust();
}
if (normalizedAngularVelocityX >= -0.0001f)
{
RB.angularVelocity = new Vector3(0.0f, normalizedAngularVelocityY, normalizedAngularVelocityZ);
}
}
}
private void NullYRotation()
{
Debug.Log("Y Vel: " + normalizedAngularVelocityY);
//Stop Left Yaw by activating the thrusters on the opposite side of the ship...
if (normalizedAngularVelocityY < 0.0f && KB.AKeyState() != true)
{
Debug.Log("Stop Yawing Left!");
if (normalizedAngularVelocityY <= 0.0f)
{
thrusters[1].ApplyThrust();
thrusters[2].ApplyThrust();
}
if (normalizedAngularVelocityY >= -0.0001f)
{
RB.angularVelocity = new Vector3(normalizedAngularVelocityX, 0.0f, normalizedAngularVelocityZ);
}
}
//Stop Right Yaw by activating the thrusters on the opposite side of the ship...
if (normalizedAngularVelocityY > 0.0f && KB.DKeyState() != true)
{
Debug.Log("Stop Yawing Right!");
if (normalizedAngularVelocityY >= 0.0f)
{
thrusters[0].ApplyThrust();
thrusters[3].ApplyThrust();
}
if (normalizedAngularVelocityY <= 0.0001f)
{
RB.angularVelocity = new Vector3(normalizedAngularVelocityX, 0.0f, normalizedAngularVelocityZ);
}
}
}
private void YawControl()
{
//Yaw Left
if (KB.AKeyState() == true)
{
thrusters[0].ApplyThrust();
thrusters[3].ApplyThrust();
}
//Yaw Right
if (KB.DKeyState() == true)
{
thrusters[1].ApplyThrust();
thrusters[2].ApplyThrust();
}
}
private void PitchControl()
{
//Pitch down
if (KB.WKeyState() == true)
{
thrusters[4].ApplyThrust();
thrusters[5].ApplyThrust();
thrusters[10].ApplyThrust();
thrusters[11].ApplyThrust();
}
//Pitch Up
if (KB.SKeyState() == true)
{
thrusters[6].ApplyThrust();
thrusters[7].ApplyThrust();
thrusters[8].ApplyThrust();
thrusters[9].ApplyThrust();
}
}
}

