Working out Difference in Rotation

Hey UA,

I've been working on a script that allows the player to use a device to look up and down. It works fine but my issue is that, because it locks the angle that they can look between, when you change direction it always flips to look down.

I'm guessing that I need to work out the difference between the angle before and after they turn around (so if they are looking straight ahead at 180, when they go the other way it changes to -180). But my maths skills are poor and don't know where to start! Anyone got any ideas?

`

var yRotation : float; var h : float; var facingRight : boolean = false;

function Update () {

h = Input.GetAxis("Horizontal");

if (h < 0) {
    facingRight = false;
} else if (h > 0) {
    facingRight = true;
}

if (facingRight == false) {
    yRotation -= Input.GetAxisRaw("Mouse Y")*3;
} else if (facingRight == true) {
    yRotation += Input.GetAxisRaw("Mouse Y")*3;
}

transform.eulerAngles = Vector3(0, 0, yRotation);

//lock rotation     
if(facingRight==false) yRotation = Mathf.Clamp(yRotation, -205, -150);
if(facingRight==true) yRotation = Mathf.Clamp(yRotation, -25, 30);

} `

Try transform.localEulerAngles instead. That way it'll be rotationally invariant.

Solved my problem with the following code:

` var yRotation : float; var h : float;

function Update () {

h = Input.GetAxis("Horizontal");

//inverts, adjust multiplication to tweak sensitivity
yRotation -= Input.GetAxisRaw("Mouse Y")*4;
transform.localEulerAngles = Vector3(yRotation, 0, 0);

//lock rotation
yRotation = Mathf.Clamp(yRotation, -33, 20);    

} `

Thanks so much flaviusxvii!