Removing the tilt from a 3d rotation

I’m at my whits end. I’ve searched through the forums and answers, and found lots of suggestions, but for whatever reason none of them work on my project :-/

I have a spaceship that moves around in 3d space. It has up/down rotation, left/right rotation, and thrust. For whatever reason, as I’m rotating the ship around, it tilts (I believe on the Z axis). I’m including the script, that has commented out some of the things that I’ve tried. I also have the “Z” axis checked on the Constraints on the Rigid Body. Any ideas what I’m doing wrong?

#pragma strict

var accel : float= 5;
var burnAccel : float = 20;
var mainThrust : ParticleEmitter;
var backRightThrust : ParticleEmitter;
var frontRightThrust : ParticleEmitter;
var backLeftThrust : ParticleEmitter;
var frontLeftThrust : ParticleEmitter;
var bottomThrust : ParticleEmitter;
var topThrust : ParticleEmitter;
var mainCamera : Camera;
var defaultFOV : int;
var rotSpeed : float;

function Start () 
{

}

function Update () 
{
	
	var xVelSq : int = (Mathf.Abs(rigidbody.velocity.x * rigidbody.velocity.x));
	var yVelSq : int = (Mathf.Abs(rigidbody.velocity.y * rigidbody.velocity.y));
	var zVelSq : int = (Mathf.Abs(rigidbody.velocity.z * rigidbody.velocity.z));
	
	var meanVelocity : float = (Mathf.Sqrt(xVelSq + yVelSq + zVelSq));
	
	//mainCamera.fieldOfView = defaultFOV + meanVelocity;
	

	if (Input.GetKey("q"))
	{
		rigidbody.AddRelativeForce(0,0, accel);
		backRightThrust.emit = true;
		backLeftThrust.emit = true;
		frontRightThrust.emit = false;
		frontLeftThrust.emit = false;
	}
	else if (Input.GetKey("z"))
	{
		rigidbody.AddRelativeForce(0,0, -accel);
		backRightThrust.emit = false;
		backLeftThrust.emit = false;
		frontRightThrust.emit = true;
		frontLeftThrust.emit = true;
	}
	else
	{
		backRightThrust.emit = false;
		backLeftThrust.emit = false;
		frontRightThrust.emit = false;
		frontLeftThrust.emit = false;
	}
	
	if(Input.GetKey("a"))
	{
		backRightThrust.emit = true;
		transform.Rotate(0,-rotSpeed,0,Space.World);
	}
	if(Input.GetKey("d"))
	{
		backLeftThrust.emit = true;
		transform.Rotate(0,rotSpeed,0, Space.World);
	}
	
	if(Input.GetKey("w"))
	{
		bottomThrust.emit = true;
		topThrust.emit = false;
		transform.Rotate(-rotSpeed,0,0, Space.World);
	}
	else if(Input.GetKey("s"))
	{
		bottomThrust.emit = false;
		topThrust.emit = true;
		transform.Rotate(rotSpeed,0,0,Space.World);
	}
	else
	{
		bottomThrust.emit = false;
		topThrust.emit = false;
	}
	
	if (Input.GetKey("e"))
	{
		mainThrust.emit = true;
		rigidbody.AddRelativeForce(0,0,burnAccel);
	}
	else mainThrust.emit = false;
	
	//transform.eulerAngles.z = 0;
	//transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
	
	//print (transform.eulerAngles);
	
	if(Input.GetKey("space"))
	{
		rigidbody.velocity = Vector3(0,0,0);
	}
	//print(rigidbody.velocity + ", " + meanVelocity);
}

For your w and s input Rotate calls, try changing the Space to Self.

The reason I suggest this, is that you should be adjusting yaw based on the based on the World’s y (vertical) axis, because left and right rotation should be constantly aligned to the world’s notion of left and right rotation.
However as you adjust the yaw, the axis you want to pitch (or even roll) around becomes unaligned with the World x(or z) axis.

That did the trick. Thank you :smile: