2025-10-15 21:31:13 +08:00
|
|
|
|
using UnityEngine;
|
2025-10-17 15:10:19 +08:00
|
|
|
|
using Interface;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
using System;
|
2025-10-17 15:10:19 +08:00
|
|
|
|
using Script.Gameplay.Input;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
|
|
|
|
|
namespace Gameplay.Player
|
|
|
|
|
{
|
|
|
|
|
public class PlayerInteractorController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private float interactRange = 15f;
|
|
|
|
|
[SerializeField] private LayerMask interactableLayer;
|
|
|
|
|
[SerializeField] private Camera playerCamera;
|
2025-10-17 15:10:19 +08:00
|
|
|
|
[SerializeField] private bool isDrawGizmos;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
|
2025-10-17 15:10:19 +08:00
|
|
|
|
private IInteractable currentTarget; // 被射线命中的当前可交互对象(用于按键交互)
|
|
|
|
|
private IInteractable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
|
2025-10-15 21:31:13 +08:00
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
|
if (playerCamera == null)
|
|
|
|
|
playerCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
|
|
|
|
|
|
|
|
|
|
var input = InputManager.Instance.Input;
|
|
|
|
|
input.Player.SwitchWatchMode.performed += ctx => currentTarget.Interact(this.gameObject);;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
DetectInteractable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DetectInteractable()
|
|
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
|
if (playerCamera == null) return;
|
|
|
|
|
|
2025-10-15 21:31:13 +08:00
|
|
|
|
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
|
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, interactRange, interactableLayer))
|
|
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
|
IInteractable hitInteractable = hit.collider.GetComponent<IInteractable>();
|
|
|
|
|
|
|
|
|
|
// 如果命中对象与之前注视的不一样,触发进入/离开事件
|
|
|
|
|
if (hitInteractable != previousGazedTarget)
|
2025-10-15 21:31:13 +08:00
|
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
|
if (previousGazedTarget != null)
|
|
|
|
|
{
|
|
|
|
|
previousGazedTarget.OnGazeExit(this.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hitInteractable != null)
|
|
|
|
|
{
|
|
|
|
|
hitInteractable.OnGazeEnter(this.gameObject);
|
|
|
|
|
// 这里可以显示交互提示UI,例如 “E - 开门”
|
|
|
|
|
Debug.Log(hitInteractable.GetInteractPrompt());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
previousGazedTarget = hitInteractable;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
}
|
2025-10-17 15:10:19 +08:00
|
|
|
|
|
|
|
|
|
currentTarget = hitInteractable;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
|
// 没有命中时,如果之前有注视对象,触发离开
|
|
|
|
|
if (previousGazedTarget != null)
|
|
|
|
|
{
|
|
|
|
|
previousGazedTarget.OnGazeExit(this.gameObject);
|
|
|
|
|
previousGazedTarget = null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 21:31:13 +08:00
|
|
|
|
currentTarget = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-17 15:10:19 +08:00
|
|
|
|
|
2025-10-15 21:31:13 +08:00
|
|
|
|
void OnDrawGizmos()
|
|
|
|
|
{
|
2025-10-17 15:10:19 +08:00
|
|
|
|
if (isDrawGizmos) return;
|
2025-10-15 21:31:13 +08:00
|
|
|
|
if (playerCamera == null) return;
|
|
|
|
|
Gizmos.color = Color.green;
|
|
|
|
|
Vector3 origin = playerCamera.transform.position;
|
|
|
|
|
Vector3 direction = playerCamera.transform.forward * interactRange;
|
|
|
|
|
Gizmos.DrawLine(origin, origin + direction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|