45 lines
1.0 KiB
C#
45 lines
1.0 KiB
C#
using UnityEngine;
|
|
|
|
public class LookWithMouse : MonoBehaviour
|
|
{
|
|
private const float k_MouseSensitivityMultiplier = 0.01f;
|
|
|
|
public float mouseSensitivity = 100f;
|
|
|
|
public Transform playerBody;
|
|
|
|
private float xRotation;
|
|
|
|
private void Start()
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
bool flag = false;
|
|
float num = Input.GetAxis("Mouse X") * mouseSensitivity * 0.01f;
|
|
float num2 = Input.GetAxis("Mouse Y") * mouseSensitivity * 0.01f;
|
|
bool keyDown = Input.GetKeyDown(KeyCode.Escape);
|
|
flag = Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1);
|
|
if (keyDown)
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
if (flag)
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
}
|
|
if (Cursor.lockState == CursorLockMode.Locked)
|
|
{
|
|
xRotation -= num2;
|
|
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
|
|
base.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
|
playerBody.Rotate(Vector3.up * num);
|
|
}
|
|
}
|
|
}
|