Your Look method currently has a few issues related to how it handles mouse input for looking around. It seems you intended to adjust both the X (for looking up and down) and Y rotation (for turning left and right), but there's an inconsistency in your implementation. Let's correct this:
User
if player mouse speed = 0 then it turns all rotation to zero
Assistant
If setting the mouse speed (sensitivity, denoted by sens in your code) to 0 results in all rotation being set to zero, then it seems like your intention is to disable looking around when the is 0.
Separate X and Y Rotation Logic: Your current implementation incorrectly updates _xRotation for both vertical and horizontal movements. You need a separate variable for tracking the Y rotation which is applied to the player's transform (for turning left and right).
Vertical Look Clamp: The clamp for vertical looking (up and down) seems misplaced. The intent is to prevent the camera from rotating too far up or down. This should only apply to the _xRotation (vertical look), not the horizontal rotation.
Correct Rotation Application: You are applying the horizontal rotation to the Y-axis of the camera rather than the character transform for turning left and right.
Here's the corrected Look method:
private void Look()
{
// Calculate rotation around the X-axis (Vertical look)
float mouseY = _input.Look.y * Time.deltaTime * sens;
_xRotation -= mouseY; // Subtract to invert the vertical input, if necessary
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f); // Clamp to prevent flipping
// Apply vertical rotation to the camera
camera.transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
// Calculate and apply rotation around the Y-axis (Horizontal turn)
float mouseX = _input.Look.x * Time.deltaTime * sens;
_yRotation += mouseX; // Adjust for smooth turning
transform.rotation = Quaternion.Euler(0f, _yRotation, 0f); // Apply Y rotation to player, not the camera.
}
Here are the key changes:
Separated the calculations for vertical (mouseY) and horizontal (mouseX) rotations.
Applied the clamp only to the vertical rotation (_xRotation), which affects looking up and down. This prevents the camera from rolling over.
Kept track of the horizontal rotation (_yRotation) within the player's transform, affecting left and right turns, rather than directly manipulating the camera's Y-axis. This ensures that the character's body turns, which is typical for first-person controllers.
Note: Make sure sens is properly initialized with a sensible value to control the sensitivity of the look motion. If it's too low or too high, looking around may feel sluggish or too fast.
sens
However, the way the Look() method is currently set up, reducing sens to 0 will indeed stop the player from being able to look around, but it won't "reset" or "zero" existing rotations. It simply means there will be no further input affecting the rotation as the mouse movement multiplied by zero results in no change. Existing rotation angles (_xRotation and _yRotation) will remain as they were the moment the sensitivity became 0.
If the desired behavior is to reset all rotation to zero (i.e., look straight forward) whenever the sens is set to 0, you would want to explicitly set the rotations to zero when this condition is met. This could be done in your Look() method with an additional check:
private void Look()
{
if (sens == 0)
{
// Reset the rotations
_xRotation = 0f;
_yRotation = transform.eulerAngles.y; // Keep the y rotation as is to prevent snapping.
camera.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
return; // Skip the rest of the method
}
// Calculate rotation around the X-axis (Vertical look)
float mouseY = _input.Look.y * Time.deltaTime * sens;
_xRotation -= mouseY; // Subtract to invert the vertical input, if necessary
_xRotation = Mathf.Clamp(_xRotation, -90f, 90f); // Clamp to prevent flipping
// Apply vertical rotation to the camera
camera.transform.localRotation = Quaternion.Euler(_xRotation, 0f, 0f);
// Calculate and apply rotation around the Y-axis (Horizontal turn)
float mouseX = _input.Look.x * Time.deltaTime * sens;
_yRotation += mouseX;
transform.rotation = Quaternion.Euler(0f, _yRotation, 0f); // Apply Y rotation to player
}
In the above example, when sens is 0, _xRotation is set to 0 to look straight ahead, and the camera's local rotation is reset accordingly. I've assumed here that when you say "turns all rotation to zero", you're specifically referring to looking straight forward rather than resetting the player's yaw (_yRotation). If you indeed wanted to reset the player's yaw orientation to a default value (e.g., facing a specific direction like North) whenever sens becomes 0, you could adjust _yRotation to that specific value instead of opting not to change it.