Hi,
I’ve been trying to get a spaceship-like flight thing off the ground for a couple of days, and after struggling with quaternions for a while and the obscene problem of gimbal lock, I’ve simplified everything to just nested gameobjects.
What I have at the moment is quite simple: an object (with camera attached) for Yaw (rotating around Y axis) under a parent for pitch (rotating around X axis). Miraculously, this simple setup avoids gimbal lock (unless I’m being really stupid).
The issue now is that I want to be able to move the entire parent-child combo (which will eventually get more complicated with the addition of pitch into the mix) according to the direction the child Z is facing.
Here is the basic script attached to child:
var yawSpeed : float = 0;
function Update () {
transform.Rotate (0,yawSpeed,0);
}
And the script attached to parent:
var pitchSpeed : float = 0;
var mainShip : Transform;
var thrustSpeed : float = 0;
function Update () {
transform.Rotate(pitchSpeed,0,0);
var newDirection : Vector3 = mainShip.TransformDirection(Vector3.forward);
transform.Translate(newDirection * thrustSpeed);
}
I assumed that what I needed was the TransformDirection, to read what the child (mainShip)'s Z direction was and apply a translate in that direction. When just adjusting the yawSpeed while playing, so the ship turns left and right, if I then add thrustSpeed it does indeed work: it sends the entire parent-child along the z-direction of the child. But when I add pitch changes, it goes wild. In fact, when the pitch is either 0 or 90, the ship moves backwards and forwards, when it is at 45 degrees, it moves directly up and down. In other words, it doesn’t seem to pay attention to the z-direction of the child. Obviously I’m doing something wrong.
Any suggestions?
Many thanks
S