I’m trying to not allow my player to fully turn when he is sitting down but can’t get it to work. I have the up/down axis working and restricts it to -35, 35, but when trying to restrict the left/right axis it doesn’t work and no errors are thrown. Here is my code with the left/right axis stuff commented out.
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
private float xRotation = 0f;
float yRotation = 0f;
// Start is called before the first frame update
void Start()
{
//removes cursor from view
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
//set the mouse x&y movement
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
//decrease xRotation based on mouseY
xRotation -= mouseY;
if (PlayerMovement.isSitting)
{
//restricts the player from turning their head too far up/down
xRotation = Mathf.Clamp(xRotation, -35, 35);
// yRotation = Mathf.Clamp(yRotation, -35, 35);
}
else
{
xRotation = Mathf.Clamp(xRotation, -60f, 60f);
// yRotation = 0f;
}
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
//move player left/right when mouse is going across x axis
playerBody.Rotate(Vector3.up * mouseX);
}
}