# pitch rotation

Hi again.

im fighting with rotations in objects local space and anything i try dont want to work. for a while i thought i understand the logic but it seems i was wrong.

here is a sample code:

``````using UnityEngine;
using System.Collections;

public class ShipMouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

float rotationX = 0F;
float rotationY = 0F;

void Update ()
{
if (axes == RotationAxes.MouseXAndY && !Input.GetKey (KeyCode.LeftControl)){

/*
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
*/

/*
rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
rotationY = transform.localEulerAngles.x + Input.GetAxis ("Mouse Y") * sensitivityY;
transform.localEulerAngles = new Vector3 (rotationY, rotationX, 0);
*/

/*
transform.RotateAround(transform.position, transform.TransformDirection(transform.up),
Input.GetAxis ("Mouse X") * sensitivityX * Time.deltaTime);
transform.RotateAround(transform.position, transform.TransformDirection(transform.right),
Input.GetAxis ("Mouse Y") * sensitivityY * Time.deltaTime);
*/

/*
transform.Rotate(transform.up, Input.GetAxis ("Mouse X")
* sensitivityX * Time.deltaTime, Space.Self);
transform.Rotate(transform.right, Input.GetAxis ("Mouse Y")
* sensitivityY * Time.deltaTime, Space.Self);
*/

/*
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
transform.localRotation = Quaternion.Euler(rotationY, rotationX, 0);
*/

/*
rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
transform.localRotation = Quaternion.identity
* Quaternion.Euler(0, rotationX, 0)
* Quaternion.Euler(rotationY, 0, 0);
*/
}

if(Input.GetKeyUp (KeyCode.LeftControl)) {
Screen.lockCursor = true;
Screen.showCursor = false;
} else if (Input.GetKeyDown (KeyCode.LeftControl)) {
Screen.lockCursor = false;
Screen.showCursor = true;
}
}

void Start ()
{

}
}
``````

there are a couple of “tries” as you can see and none of them works.

problem with “rotate” and “rotatearound” is that the object has SOME rotation around Z axis too, I cant understand why

every try with eulerangles failed as when the object look stright up, it rotates the object around world Y instead of local Y

I start to give it up… help pls

Take a look at this thread. Pretty straightforward.

Edit:

After the back and forth, I understand what the problem is. If you rotate around 2 axis that move together, you can actually achieve any rotation on any 3 axis (try it with your hand, you’ll understand quickly).

What you’ll want to do is rotate in 2 steps and relative to different spaces:

``````transform.Rotate(
0f,
Input.GetAxis ("Mouse X") * sensitivityX * Time.deltaTime,
0f,
Space.World);
transform.Rotate(
Input.GetAxis ("Mouse Y") * sensitivityY * Time.deltaTime,
0f,
0f,
Space.Self);
``````

Notice one rotation is relative to Space.World and the other to Space.Self

Edit2:

Controlling the spaceship on all 3 axis feels much more natural and let’s you rotate all axis based on Space.Self. Here’s the code:

(and what it looks like here, camera movements and all)

``````using UnityEngine;
using System.Collections;

public class Rotater : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityRoll = 45F;
public float sensitivityPitch = 30F;
public float sensitivityYaw = 35f;

float rotationX = 0F;
float rotationY = 0F;

void Update ()
{
if (axes == RotationAxes.MouseXAndY && !Input.GetKey (KeyCode.LeftControl)){

transform.Rotate(
Input.GetAxis ("Mouse Y") * sensitivityPitch * Time.deltaTime,
Input.GetAxis ("Mouse X") * sensitivityYaw * Time.deltaTime,
Input.GetAxis ("Mouse X") * -sensitivityRoll * Time.deltaTime,

Space.Self);

}

if(Input.GetKeyUp (KeyCode.LeftControl)) {
Screen.lockCursor = true;
Screen.showCursor = false;
} else if (Input.GetKeyDown (KeyCode.LeftControl)) {
Screen.lockCursor = false;
Screen.showCursor = true;
}
}

}
``````

Hope I understood you correctly, but the problem is exactly that you are trying to rotate around two local axis. Because it changes everytime you rotate, sooner or later the global Z axis will change too. Just try and replace the localEulerAngles with this:

``````transform.rotation = Quaternion.Eulers(rotationY,rotationX,0);
``````

Hope this is what you wanted