using UnityEngine;

public class ThirdPersonCam : MonoBehaviour
{
    [Header("References")]
    public Transform orientation;
    public Transform player;
    public Transform playerObj;
    public Rigidbody rb;

    public float rotationSpeed;


    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        
        rb.freezeRotation = true;
    }

    private void Update()
    {
        // -------- Camera → Orientation (Y-axis only) --------
        Vector3 viewDir = player.position - transform.position;
        viewDir.y = 0f;

        orientation.forward = viewDir.normalized;

        // -------- Player Rotation --------
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 inputDir = orientation.forward * verticalInput + orientation.right * horizontalInput;
        inputDir.y = 0f; // EXTRA SAFETY

        if (inputDir.sqrMagnitude > 0.01f)
        {
            Quaternion targetRotation = Quaternion.LookRotation(inputDir);
            playerObj.rotation = Quaternion.Slerp(
                playerObj.rotation,
                targetRotation,
                rotationSpeed * Time.deltaTime
            );
        }
    }
}
