Can someone tell me how this code works?

Hello! I found this code on the forums but I don’t understand it fully on why it works. I want to understand why it works so next time I do this I know how to do it and why it works this way. Here is the code:

    float mouseX = (Input.mousePosition.x / Screen.width) - 0.5f;
    float mouseY = (Input.mousePosition.y / Screen.height) - 0.5f;
    transform.localRotation = Quaternion.Euler(new Vector4(-1f * (mouseY * 180f), mouseX * 360f, transform.localRotation.z));

This goes in the update and what it does is make the camera move to your mouse… Like an fps game.
I want to know why we divide the mouse input by the Screen width/height and why subtract it by 0.5…
And why we do that math in the Quaternion.Euler to make it work properly. Also I have no idea how to add sensitivity to this. Anyone feel free to answer I simple want to learn! Thanks!

Input.mousePosition.x is defined in the Screen space, i.e, it can take values between 0 and Screen.width. Dividing Input.mousePosition.x by Screen.width returns a value clamped between 0 and 1. Substracting by 0.5 “shifts” the range between -0.5 and 0.5

The same operations are performed on Input.mousePosition.y.

Then, the rotation of the object is performed using euler angles:

The angle around the pitch axis ( x axis in Unity) is computed according to the mouse position on the vertical axis: -1f * (mouseY * 180f) means that the object will rotate “up” or “down” between 90° (if mouseY = -0.5, meaning the mouse in at the bottom of the screen), and -90° (if mouseY = 0.5, meaning the mouse in at the top of the screen)

The angle around the yaw axis ( y axis in Unity) is computed the same way : the object will rotate “left” or “right” between -180° (if mouseX = -0.5, meaning the mouse in at the left side of the screen), and 180° (if mouseX = 0.5, meaning the mouse in at the right side of the screen)

The final angle around the roll axis is kept as it is.

Finally, the computed euler angles are converted into a Quaternion using Quaternion.Euler and becomes the new local rotation of the object.

X axis : [0, Screen.width] → [0,1] → [-0.5, 0.5] → [-90, 90]

Y axis : [0, Screen.height] → [0,1] → [-0.5, 0.5] → [-180, 180]