UnityBiz/Assets/scripts/Camera/PlayerCameraController.cs
adamwi1000 b854205dcb test
2024-08-02 06:41:41 +02:00

64 lines
1.9 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
namespace Player.Camera
{
public class PlayerCameraController : MonoBehaviour
{
[Header("References")]
[SerializeField] private Transform cameraTargetPosition;
[SerializeField] private Transform cameraTransform;
[Space]
[Header("Settings")]
[SerializeField] private PlayerCameraSettings playerCameraSettings;
[SerializeField] private InputActionReference lookAction;
public PlayerCameraSettings PlayerCameraSettings => playerCameraSettings;
public Vector3 CameraRotationVector { get; private set; } = Vector3.forward;
public Transform CameraTargetTransform => cameraTargetPosition;
public Transform CameraTransform => cameraTransform;
private void OnEnable()
{
lookAction.action.performed += OnLook;
}
private void OnDisable()
{
lookAction.action.performed -= OnLook;
}
private void OnLook(InputAction.CallbackContext obj)
{
var mouseDelta = obj.ReadValue<Vector2>();
mouseDelta *= PlayerCameraSettings.MouseSensitivity;
var x = mouseDelta.x;
var y = mouseDelta.y;
var clampedY = Mathf.Clamp(CameraRotationVector.x - y, PlayerCameraSettings.MinAngle, PlayerCameraSettings.MaxAngle);
CameraRotationVector = new Vector3(clampedY, CameraRotationVector.y + x, 0);
}
private void LateUpdate()
{
cameraTransform.rotation = Quaternion.Euler(CameraRotationVector);
cameraTransform.position = cameraTargetPosition.position;
}
public void LookAt(Vector3 target)
{
throw new System.NotImplementedException();
}
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
}
}