c# - How to constrain rotation from mouse input in unity 5? -


i have basic script move camera mouse position, want constrain rotation of z axis values. code below works fine, camera can rotated on z axis, want limit 20 , -40. have tried use mathf.clamp didn't work , when printed console printed out right value in mathf.clamp. tried using if statement see if rotation on limit, , reset if was. neither worked... have looked around on unity answers don't understand of other answers, show me how it?

code:

void update () {         transform.rotation = quaternion.euler(0f, input.mouseposition.x, input.mouseposition.y);     } 

this how clamp it.

void update() {     float zrotation = mathf.clamp(input.mouseposition.y, -40, 20);     transform.rotation = quaternion.euler(0f, input.mouseposition.x, zrotation); } 

but don't think want. mouse position given in window coordinates, won't ever have negative values. you'll want translate coordinates first, this:

void update() {     float yrotation = (input.mouseposition.x - screen.width / 2) * 360 / screen.width;     float zrotation = (input.mouseposition.y - screen.height / 2) * 180 / screen.height;     zrotation = mathf.clamp(zrotation, -40, 20);     transform.rotation = quaternion.euler(0f, yrotation, zrotation); } 

Comments