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!
Answer by Hellium · Aug 14, 2018 at 07:19 PM
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]
@Hellium Wow nicely explained! Thank you. One question though. What if I want to lock the mouse? This will no longer work because it is locked to the center I assume. Any ideas on that or should I just hide the mouse? Either way the user will be forced to look around with the limits of their screen. This is because when the mouse hits the edge of the screen there is no more movement of the camera even if I keep moving the mouse as if I wanted to keep turning my head. I'm not sure what to do at this point. Thanks!
I guess you can try something like the following (code not tested)
private Vector2 mousePosition = Vector2.one * 0.5f;
private void Start()
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
}
private void Update()
{
mousePosition.x = $$anonymous$$athf.Clamp( mousePosition.x + Input.GetAxis("$$anonymous$$ouse X"), 0, Screen.width ) ;
mousePosition.y = $$anonymous$$athf.Clamp( mousePosition.y + Input.GetAxis("$$anonymous$$ouse Y"), 0, Screen.height ) ;
// The code you currently have
// Just replace `Input.mousePosition` by `mousePosition`
}
@Hellium Great fix but now I have another issue! So you know how the camera goes up and down? You can basically go around as many times as you want. I tried a mathf.clamp and it works but then we have the issue of the mouse input. If the mouse input goes over 90 or -90 the camera clamps but now if you are at -90 (Looking up) and want to look down you have to keep moving your mouse down until you hit -90 and lower depending on how far you moved your mouse over -90 even though its clamped. If that made any sense.
Your answer
Follow this Question
Related Questions
Unity 3D: Third person movement and mouse camera control 0 Answers
Mouse Look Script Not Working 1 Answer
New Input System first person camera stutter. 0 Answers
Object Rotate with Camera 2 Answers
drag AND drop push and pull object 0 Answers