# Transform.forward having problems after rotation

I have this script,

void Update () {

float pitch, yaw, roll;

pitch = Input.GetAxis ("Mouse Y");
yaw = Input.GetAxis ("Mouse X");
roll=Input.GetAxis ("Roll");

transform.Rotate (-pitch, yaw, -roll);

if (Input.GetKey (KeyCode.W))
Rigid.AddRelativeForce (transform.forward * 50);
else if (Input.GetKey (KeyCode.S))
Rigid.AddRelativeForce (-transform.forward * 50);
else if (Input.GetKey (KeyCode.D))
Rigid.AddRelativeForce (transform.right * 50);
else if (Input.GetKey (KeyCode.A))
Rigid.AddRelativeForce (-transform.right * 50);
else
Rigid.velocity = Vector3.zero;

if (Input.GetKey (KeyCode.Space)) {

transform.rotation = Quaternion.Euler (Vector3.zero);
Rigid.velocity=Vector3.zero;
Rigid.angularVelocity=Vector3.zero;
}

}


My "Roll" is defined as 'q' and 'e' keys. "Rigid" is rigidbody object.

I simply want to implement 6DoF on this cube, the pitch, yaw and roll works fine , but when it comes to movement it behaves in very unusual way after the object is rotated, I tried moving by using localPosition still no result. I also used relative torque for rotation but it resulted in worse, that is why rotation is controlled by transform.rotation not torque.

I suppose I am not handling gimbal lock or something? Or am I doing it completely wrong?

• Please define what you mean by a "very unusual way," ideally with images or video, and describe or show how this deviates from the behaviour that you want. If you don't tell us the symptoms, we can't help you diagnose the illness. – DMGregory Oct 12 '15 at 21:04
• Here, the Video (dropbox.com/s/xzxfwws088on397/Rotation.3gp?dl=0) One more thing to note here is that, if it is moving with respect to global space, it is still not following the axes properly. – Talha Oct 13 '15 at 7:13

You can fix this by either sticking with AddRelativeForce and replacing transform with Vector3, or by keeping transform.forward/right but using Rigidbody.AddForce instead.