106 lines
3.9 KiB
C#
106 lines
3.9 KiB
C#
using UnityEngine;
|
|
using OnlyScove.Scripts;
|
|
using Invector.vCamera;
|
|
|
|
namespace OnlyScove.Player
|
|
{
|
|
public class PlayerInteraction : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
[SerializeField] private float interactDistance = 4f;
|
|
[SerializeField] private LayerMask interactableLayer;
|
|
[SerializeField] private KeyCode interactKey = KeyCode.E;
|
|
|
|
private Camera mainCam;
|
|
private IInteractable currentInteractable;
|
|
|
|
private void Start()
|
|
{
|
|
FindCamera();
|
|
}
|
|
|
|
private void FindCamera()
|
|
{
|
|
// 1. Tìm Invector Camera
|
|
if (vThirdPersonCamera.instance != null)
|
|
mainCam = vThirdPersonCamera.instance.GetComponent<Camera>();
|
|
|
|
// 2. Dự phòng Camera.main
|
|
if (mainCam == null) mainCam = Camera.main;
|
|
|
|
if (mainCam != null)
|
|
Debug.Log($"<color=cyan>[Interaction]</color> Đã kết nối với Camera: {mainCam.name}");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (mainCam == null || !mainCam.isActiveAndEnabled) { FindCamera(); return; }
|
|
|
|
// Vẽ tia Ray để Debug
|
|
Debug.DrawRay(mainCam.transform.position, mainCam.transform.forward * interactDistance, Color.red);
|
|
|
|
CheckInteraction();
|
|
HandleInput();
|
|
}
|
|
|
|
private void CheckInteraction()
|
|
{
|
|
// Bắn Ray từ tâm Camera (Giống logic CameraOpenDoor của Asset)
|
|
Ray ray = mainCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
|
|
RaycastHit hit;
|
|
|
|
// Bỏ qua Layer Player (8) để không bị chặn bởi lưng nhân vật
|
|
int layerMask = ~(1 << 8);
|
|
|
|
if (Physics.Raycast(ray, out hit, interactDistance, layerMask))
|
|
{
|
|
// 1. Kiểm tra Interface IInteractable (Hệ thống của chúng ta)
|
|
IInteractable interactable = hit.collider.GetComponentInParent<IInteractable>();
|
|
|
|
// 2. Kiểm tra trực tiếp script Door (Nếu là Door từ Asset Wood Door Pack)
|
|
if (interactable == null)
|
|
{
|
|
var doorScript = hit.collider.GetComponentInParent<DoorScript.Door>();
|
|
if (doorScript != null)
|
|
{
|
|
// Tự động bọc script Door của Asset vào hệ thống của chúng ta nếu nó chưa có
|
|
interactable = hit.collider.gameObject.GetComponentInParent<DoorInteractable>();
|
|
if (interactable == null)
|
|
{
|
|
// Nếu trúng Door mà chưa có DoorInteractable, báo để bạn biết
|
|
// Debug.LogWarning($"[Interaction] Trúng Door {hit.collider.name} nhưng thiếu script DoorInteractable!");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (interactable != null)
|
|
{
|
|
if (currentInteractable != interactable)
|
|
{
|
|
if (currentInteractable != null) currentInteractable.OnHoverExit();
|
|
currentInteractable = interactable;
|
|
currentInteractable.OnHoverEnter();
|
|
Debug.Log($"<color=green>[Interaction]</color> NHÌN TRÚNG: {hit.collider.gameObject.name}");
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (currentInteractable != null)
|
|
{
|
|
currentInteractable.OnHoverExit();
|
|
currentInteractable = null;
|
|
}
|
|
}
|
|
|
|
private void HandleInput()
|
|
{
|
|
if (currentInteractable != null && Input.GetKeyDown(interactKey))
|
|
{
|
|
Debug.Log("<color=yellow>[Interaction]</color> THỰC HIỆN TƯƠNG TÁC!");
|
|
currentInteractable.OnInteract(gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|