Simple rotation question when controlling object in 3D

I have a problem with rotation that I was hoping someone could point me in the right direction.

I am sure there is a much easier way to ask the question, but here is the long version.

I have a script (below) which controls an object in 3D space. I want to emulate zero gravity.

The problem I am having is that when the object is heading “north” and pitched downwards, when I rotate “south” the object rotates on it’s own Y axis rather than the world axis which results in the object pitching up when heading “south”.

Is there a simple way to cause “Horizontal” input to rotate an object using the world axis?

Thanks for any advice.
-ab

var speed : int = 10;
var gravity : int = 10;

private var moveDirection = Vector3.zero;

function FixedUpdate () {
	
	var controller :CharacterController = GetComponent(CharacterController);
	
		moveDirection = Vector3(0,0, Input.GetAxis("Vertical") * speed);
		moveDirection = transform.TransformDirection(moveDirection);
	
	
	var rotateY = (Input.GetAxis("Horizontal") * 200) * Time.deltaTime;
	var rotateX = (Input.GetAxis("Mouse Y") * -400) * Time.deltaTime;
	
	controller.transform.Rotate(rotateX,rotateY, 0);
	

	
	controller.Move (moveDirection * Time.deltaTime);
	
	
}

It sounds like you want to implement a typical first-person control scheme where pitch is always about the local side axis and yaw is always about the world up axis.

If that is the case, then I’d recommend storing the rotation as two angles (pitch and yaw), manipulating those angles in response to user input, and then rebuilding the orientation from scratch each update, e.g.:

transform.eulerAngles = new Vector3(pitch, yaw, 0f);

I’ll also mention that by using Space.World as the second argument to Rotate(), you can rotate about the parent/world axes rather than the local axes. However, for basic ‘pitch-yaw’ motion, I’d still suggest the method I described above.

Thanks, Jesse.

I’ll give it a shot.