Camera Restriction

Ok, so I’m doing a camera restriction, it restricts the player from looking too much upwards and downwards.

Here’s my code for the camera so far.

var rotation = 0.0;
var current_pitch = 0.0;


function Update () {
rotation = -Input.GetAxis("Mouse Y") * 5;
current_pitch += rotation;
transform.Rotate(rotation, 0, 0);
print(current_pitch);
if(current_pitch < -80.0)
{
//Set the angle back to -80

}

}

I don’t even know if this is the best way to check the rotation. So if there’s a better way to check the rotation of the camera, please tell me.

Now for the restriction line. I tried transform.rotation, but it uses Quaternions for rotation, so I can’t just put down “transform.rotation = -80”. I couldn’t figure out how to do this with the quaternions.

you can try this (untested)

var current_pitch = 0.0;

function Update () {
  current_pitch -= Input.GetAxis("Mouse Y") * 5;
  current_pitch = Mathf.Clamp(current_pitch, -80, 80)
  transform.eulerAngles = Vector3(current_pitch, 0, 0);
}

Yes, that really makes sense! The only problem here will be that the camera’s Y is locked to 0, so no matter how the parent player objects rotates, the camera’s Y will always be 0.

I tried this, but still it didn’t work:

transform.eulerAngles = Vector3(current_pitch, transform.rotation.y, 0);

From what I read in the scripting refference transform.rotation.y should return the object’s current Y rotation, right?

EDIT: Got it! I used localEulerAngles.