using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public float XSensitivity = 90f;
    public float YSensitivity = 90f;
    public float minimumAngle = -45f;
    public float maximumAngle = 60f;
    public float smoothness = 10f;
    public Transform cameraTrans;
    private Transform characterTrans;
    private Quaternion targetRotationX;
    private float targetRotationY;
    // Start is called before the first frame update
    void Start()
    {
        characterTrans = GetComponent<Transform>();
        targetRotationX = characterTrans.localRotation;
        targetRotationY = 0f;
    }

    // Update is called once per frame
    void Update()
    {
        float t = 0.5f + smoothness * Time.deltaTime;
        //rotate yaw
        float rotationAngle = Input.GetAxis("Mouse X") * XSensitivity * Time.deltaTime;
        targetRotationX *= Quaternion.Euler(0f, rotationAngle, 0f);
        characterTrans.localRotation = Quaternion.Slerp(characterTrans.localRotation,
                                                        targetRotationX,
                                                        t);
        
        //rotate pitch
        targetRotationY += Input.GetAxis("Mouse Y") * XSensitivity * Time.deltaTime;
        targetRotationY = Mathf.Clamp(targetRotationY,
                                        minimumAngle,
                                        maximumAngle);
        cameraTrans.localRotation = Quaternion.Slerp(cameraTrans.localRotation,
                                                    Quaternion.Euler(-targetRotationY, 0f, 0f),
                                                    t);
    }
}
