- Home /
FPS camera script - keeps reverting to 0,0,0 rotation
Hi. I've been trying to make a script for a First Person camera, and I've looked around but I can't seem to find anything that someone else has made that I can incorporate as I want. The problem I'm having is that in my script, whenever I move my mouse the rotationX and rotationY seem to keep jumping back to 0 immediately after the value changes (I can see the camera move very quickly before it goes back to 0 rotation). I kind of tried to simplify other examples of FPS scripts I've seen, but what could be missing (what is there that shouldn't be there?) that's making my variables go back to 0?
var sensitivityX:float = 15f;
var sensitivityY:float = 15f;
var rotationX:float = 0f;
var rotationY:float = 0f;
var originalrotation:Quaternion;
function start ()
{
originalrotation = transform.localrotation;
}
function Update ()
{
rotationX = Input.GetAxis("Mouse X") * sensitivityX * Time.deltaTime;
rotationY = Input.GetAxis("Mouse Y") * sensitivityY * Time.deltaTime;
xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.right);
transform.localRotation = xQuaternion * yQuaternion;
}
Answer by aldonaletto · Dec 23, 2012 at 07:55 PM
GetAxis("Mouse X/Y") functions return the mouse movement in the corresponding axis since last frame, thus you must accumulate them to get usable rotations - and since the mouse movement is already proportional to the time between frames, multiplying by Time.deltaTime isn't needed. Additionally, you should limit rotationX and rotationY to avoid other problems: rotationX must be in modulo 360 to avoid range issues with AngleAxis, and rotationY must be clamped to a range smaller than +/-90 degrees to avoid camera madness:
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = rotationX % 360;
rotationY = Mathf.Clamp(rotationY, -80, 80);
Aaagh, such a simple correction XD. Thanks! I believe that I won't be using clams due to the nature of how I plan of using the camera's view, but in case I need it that'll sure help too :D!
Your answer
Follow this Question
Related Questions
I need help with my script 3 Answers
Camera Height-based crouch script 2 Answers
how can i make my character jump? 5 Answers
Camera Bobbing and Stairs 4 Answers
I want to change gun position with my camera joystick 0 Answers