Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
1.4 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerCamera : MonoBehaviour
{
    [SerializeField] float xSens = 40f;
    [SerializeField] float ySens = 40f;
    [SerializeField] float maxAngle = 90f;
    [SerializeField] Transform orientation;

    PlayerInputActions controls;

    float xRotation;
    float yRotation;

    void Awake()
    {
        controls = new PlayerInputActions();
    }

    void OnEnable()
    {
        controls.Enable();
    }

    void OnDisable()
    {
        controls.Disable();
    }

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void Update()
    {
        Look();
    }

    void Look()
    {
        Vector2 t_mouseInput = controls.Player.Look.ReadValue<Vector2>();

        float t_mouseX = t_mouseInput.x * xSens * Time.deltaTime;
        float t_mouseY = t_mouseInput.y * ySens * Time.deltaTime;

        yRotation += t_mouseX;
        xRotation -= t_mouseY;
        xRotation = Mathf.Clamp(xRotation, -maxAngle, maxAngle);

        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0f);
        orientation.rotation = Quaternion.Euler(0f, yRotation, 0f);
    }
}