37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace IllOmen
|
|
{
|
|
/// <summary>Lets the player look around with the mouse from a fixed position.</summary>
|
|
public class LookAround : MonoBehaviour
|
|
{
|
|
[SerializeField] private float sensitivity = 0.08f;
|
|
[SerializeField] private float maxYaw = 75f;
|
|
[SerializeField] private float maxPitch = 40f;
|
|
|
|
private float yaw;
|
|
private float pitch;
|
|
private Quaternion startRotation;
|
|
|
|
private void Start()
|
|
{
|
|
startRotation = transform.rotation;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Mouse mouse = Mouse.current;
|
|
if (mouse == null) return;
|
|
|
|
Vector2 delta = mouse.delta.ReadValue() * sensitivity;
|
|
yaw = Mathf.Clamp(yaw + delta.x, -maxYaw, maxYaw);
|
|
pitch = Mathf.Clamp(pitch - delta.y, -maxPitch, maxPitch);
|
|
|
|
transform.rotation = startRotation * Quaternion.Euler(pitch, yaw, 0f);
|
|
}
|
|
}
|
|
}
|