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

public class PlayerLook : MonoBehaviour
{
    [Header("Animation")]
    public float smoothDamp = 0.3f;
    public Vector3 velocity;
    // private bool isPlayerAlive = true;

    // void Awake() {
    //     PlayerManager.I.playerDeath += OnPlayerDeath;
    // }

    // void OnDestroy() {
    //     PlayerManager.I.playerDeath -= OnPlayerDeath;
    // }

    // private void OnPlayerDeath() {
    //     isPlayerAlive = false;
    // }

    private void Update()
    {
        if(/*isPlayerAlive && */PlayerManager.I.unit != null && PlayerManager.I.look != null) {
            var player = PlayerManager.I.unit;
            var look = PlayerManager.I.look;

            player.lookTarget = look;

            var p = new Plane();
            p.normal = player.transform.up;

            var ray = CameraManager.I.rawCam.ScreenPointToRay(Input.mousePosition);

            float enter;
            if (p.Raycast(ray, out enter))
            {
                var intersect = ray.GetPoint(enter);
                if(look != null)
                    look.position = Vector3.SmoothDamp(look.position, intersect, ref velocity, smoothDamp);
            }
        }
    }
}
