- Home /
How does this first person camera script work?
I couldn't find a decent explanation for a beginner. I'm unsure about a lot of the math behind this script. It would help if someone can go through line by line starting from the Update() method.
public class NewBehaviourScript : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 5F;
public float sensitivityY = 5F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
private Rigidbody rb;
void Start()
{
if (rb)
rb.freezeRotation = true;
}
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I properly rotate an FPS camera with a joystick? 1 Answer
Rotating a camera that is already rotated on Z axis 0 Answers
Platformer game level rotation 0 Answers
Storing camera angles for a FPS -1 Answers