- Home /
How to restrict a cameras max/min rotation?
Hi!
I have this huge problem of mine, and I can't find any solution to this.. Have tried meny ways but none of them works for me.
I'm still a beginner of unity, but I will learn to use it one step at a time..
Im trying to make a FPScamera myself and The problem is that it turns all the way around when you look up.. So that you eventually end up loooking forward again.. I dont want that. I want the camera to Stop moving "Up" when it has reached the point where it's actually is looking up, at the angle of 90 degrees I think it would be. The same for looking down. So my Code is:
//Simple Cam Move
//Drag player into Player transform
//Set camera as a child of Player
var rotateSensitivity = 5; var Player : Transform;
function Update () { var vertic : float = Input.GetAxis("Mouse Y"); var horiz : float = Input.GetAxis("Mouse X");
if(Input.GetAxis("Mouse Y")){ transform.Rotate(Vector3(-vertic * rotateSensitivity,0,0)); }
if(Input.GetAxis("Mouse X")){ Player.Rotate(Vector3(0,horiz * rotateSensitivity,0)); } }
I had in mind that i could use Mathf.Clamp to restrict it But with no sucsess.. Maby something like this?
transform.rotation.x = Mathf.Clamp(transform.rotation.x, -10, 10);
Maby im just doin it wrong.. You tell me. Im toaly Stuck on this. Plz Help! Thx! //Edvin
The $$anonymous$$ouseLook script from standard assets already does $$anonymous$$/max clamping.
I dont want to use that,, And i cant figure out how to convert Cs into Js.. as I said i want to make my own $$anonymous$$ouselook.. But I cant figure out how to clamt the rotations...
I$$anonymous$$ GOING CRAZY RIGHT NOW!!!! HOW THE * DO YOU SOLVE THIS!? HAVE SIT HERE FOR HOURS NOW AND CANT FIND EVEN 1 SINGLE SOLUTION!!?? IS IT RLY THAT HARD? //ragemode... Totaly flipping out here!
It's not hard. If you're still having problems with it, I'd recommend starting a thread in the forums (be sure to include your current code and a description of how it's not working).
Answer by Jesse Anders · Dec 19, 2010 at 10:53 PM
Transform.rotation is a unit-length quaternion, so this:
transform.rotation.x = Mathf.Clamp(transform.rotation.x, -10, 10);
Won't do anything useful.
What I would recommend is to create a 'pitch' variable, adjust this variable in response to mouse input, use Mathf.Clamp() to clamp it as desired, and then each update, set the local rotation for the object like this (guessing at UnityScript syntax):
transform.localEulerAngles = Vector3(pitch, 0, 0);
Thx for the sugestion.. I will try it out.. and if it works properly I'll use it, otherwise I might have another idea to get it to work the way I want :) $$anonymous$$thxbai!
Your answer
Follow this Question
Related Questions
Camera not rotate when following player 1 Answer
Can't stop camera from rotating on Z Axis 1 Answer
Rotating Player with Mouse 0 Answers
How does this first person camera script work? 0 Answers
First Person Shooter Help With Cam 1 Answer