Airplanes - Tilt and Turn

I’ve got an airplane script working great. The arrow keys make it move up and down, the left/right keys turn it left and right.

The problem is that I want my plane to tilt as it turns. So rather than just rotating my plane around the y-axis, I want it to tilt(rotate along the z-axis). Setting rotation properties in the inspector works just fine. The problem comes when I want to rotate the plane in code.

Here is my code:

function Update () {
	if(Input.GetAxis("Horizontal") > 0.0){ //if the right arrow is pressed
		transform.Rotate(0.0, turnSpeed * Time.deltaTime, -10.0 * Time.deltaTime); //and then turn the plane
	}
	if(Input.GetAxis("Horizontal") < 0.0){ //if the left arrow is pressed
		transform.Rotate(0.0, -turnSpeed * Time.deltaTime, 10.0 * Time.deltaTime); //The X-rotation turns the plane - the Z-rotation tilts it
	}
	if(Input.GetAxis("Vertical") > 0.0){ //if the up arrow is pressed
		transform.Rotate(10.0 * Time.deltaTime, 0.0, 0.0);
	}
	if(Input.GetAxis("Vertical") < 0.0){ //if the down arrow is pressed
		transform.Rotate(-10.0 * Time.deltaTime, 0.0, 0.0);
	}
}

I would assume that this code would do the same thing as setting the same variables in the inspector, but rather than turning the plane and tilting it, it turns the plane, tilts it, and brings the nose down. Why does it do this?[/code]

have you tried to make a new Z Axis in the input properties?

I made this and its working for my spaceship…

Sorry about the late reply - my ISP is going crazy.

Anyway, after a bit more banging my head on the keyboard, I’ve narrowed down my question - how would I rotate an object(through scripting) such that it behaves like the transform gadget in the Scene view? For example, I have the following line of code:

transform.Rotate(10, 0, 0);

This code tilts my airplane as though I am using the red rotation widget in the Scene view, as long as the plane’s Y rotation is 0. But if the Y rotation is 90, it behaves as though I am using the blue rotation widget in the scene view. Can anyone help with this?

The other thing is that you want to rotate the plane horizontally around the World’s Y axis, but tilt it around the plane’s z axis. You should look at world vs local-space.

function Update () {
   if(Input.GetAxis("Horizontal") > 0.0){ //if the right arrow is pressed
      transform.Rotate(0.0, turnSpeed * Time.deltaTime, -10.0 * Time.deltaTime, Space.World); //and then turn the plane
   }
   if(Input.GetAxis("Horizontal") < 0.0){ //if the left arrow is pressed
      transform.Rotate(0.0, -turnSpeed * Time.deltaTime, 10.0 * Time.deltaTime, Space.World); //The X-rotation turns the plane - the Z-rotation tilts it
   }
   if(Input.GetAxis("Vertical") > 0.0){ //if the up arrow is pressed
      transform.Rotate(10.0 * Time.deltaTime, 0.0, 0.0);
   }
   if(Input.GetAxis("Vertical") < 0.0){ //if the down arrow is pressed
      transform.Rotate(-10.0 * Time.deltaTime, 0.0, 0.0);
   }
}

Thanks for the reply!

Once again, I’ve found the solution by doing something I (thought) I had already done. I set it up using eulerAngles, and it now works fine. I’ll take a look into the code you posted and see if it will work any better.

Once again, thanks!

Dude, You are the man! The horseman! I applied your stuff just now blindly and it works lol; I will try to understand whats going on… love u!