- Home /
How do I properly rotate an FPS camera with a joystick?
So I've been looking around for quite a while now and it seems like there is no commonly understood way of using a joystick for FPS look controls. While I could find an endless number of FPS movement scripts and tutorials for mouse and keyboard I can not find one reliable script or tutorial to help me understand how to program look controls on a gamepad.
Here is the best I have come up with so far:
originalRotation = transform.rotation;
playerRotation = Quaternion.Euler(rightJoystickDirection.y, rightJoystickDirection.x, 0.0f) * originalRotation;
transform.rotation = playerRotation;
Obviously this isn't right, and it doesn't even come close to working right, but again I'm at a loss as to how to use quaternions to rotate the player correctly.
What I really want to do is use a 2D vector (right joystick input) to rotate the camera a certain amount every second.
If anyone can point me to example scripts or tutorials that I can study to help get me closer to understanding what I need to know I'd be delighted.
Thanks!
do you want the A$$anonymous$$OUNT of joystick lean to be the SPEED OF ROTATION? or do you want it to be (I feel this is more normal) that "any" joystick lean means it rotates that way (left or right) at some certain speed?
Well once I finish this I actually want to have the amount of joystick lean (0 to 1) to feed into an exponential function that gives the speed of rotation a smooth ramp (this is how the best joystick-based shooters work like COD, full joystick deflection is orders of magnitude faster than say 30% deflection).
But I would be happy getting the rotation to work correctly at all right now ( a fixed speed would be fine).
sure, all you had to say was "amount of lean" :)
Answer by Falcon_DZ · Dec 02, 2015 at 09:47 AM
Rotation of FPS camera using right analog stick. i have a script for the camera rotation but have some issues with it. the camera automatically rotates and never comes to rest . i tried to manipulate my script but still get the same result. here what i have done!
private void tryjoy ()
{
Vector2 joyInput = new Vector2 (Input.GetAxis ("4th axis"),
Input.GetAxis ("5th axis"));
float camX = _camera.transform.localEulerAngles.x;
if ((camX > 280 && camX <= 360) || (camX >= 0 && camX < 80) || (camX >= 80 && camX < 180 && joyInput.y > 0) || (camX > 180 && camX <= 280 && joyInput.y < 0)) {
_camera.transform.localEulerAngles += new Vector3 (-joyInput.y * lookSpeed * .7f, _camera.transform.localEulerAngles.y,
_camera.transform.localEulerAngles.z);
}
transform.localEulerAngles -= new Vector3 (0, joyInput.x * lookSpeed, 0);
_yRotation = joyInput.y;
_cameraRefocus.GetFocusPoint ();
}