refactor(): 使用第一人称射线检测来替换掉 可交互和可编辑种重合的部分
This commit is contained in:
@@ -7,10 +7,7 @@ namespace Gameplay.Player
|
||||
{
|
||||
public class PlayerInteractorController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float interactRange = 15f;
|
||||
[SerializeField] private LayerMask interactableLayer;
|
||||
[SerializeField] private Camera playerCamera;
|
||||
[SerializeField] private bool isDrawGizmos;
|
||||
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
|
||||
[SerializeField] private bool isEnablePlayerInteraction = true; // 是否启用玩家交互功能
|
||||
|
||||
private IInteractable currentTarget; // 被射线命中的当前可交互对象(用于按键交互)
|
||||
@@ -18,8 +15,12 @@ namespace Gameplay.Player
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (playerCamera == null)
|
||||
playerCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
|
||||
if (raycaster == null)
|
||||
raycaster = GetComponent<FirstPersonRaycaster>() ?? GetComponentInChildren<FirstPersonRaycaster>();
|
||||
if (raycaster == null)
|
||||
raycaster = FindObjectOfType<FirstPersonRaycaster>();
|
||||
if (raycaster == null)
|
||||
Debug.LogWarning("FirstPersonRaycaster not found! Please assign or add it to the player.");
|
||||
|
||||
var input = InputManager.Instance.Input;
|
||||
input.Player.Interact.performed += ctx =>
|
||||
@@ -38,45 +39,31 @@ namespace Gameplay.Player
|
||||
|
||||
void DetectInteractable()
|
||||
{
|
||||
if (playerCamera == null) return;
|
||||
if (raycaster == null) return;
|
||||
if (!isEnablePlayerInteraction) return;
|
||||
|
||||
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, interactRange, interactableLayer))
|
||||
GameObject lookAtObj = raycaster.CurrentLookAtObject;
|
||||
IInteractable hitInteractable = lookAtObj != null ? lookAtObj.GetComponent<IInteractable>() : null;
|
||||
|
||||
// 如果命中对象与之前注视的不一样,触发进入/离开事件
|
||||
if (hitInteractable != previousGazedTarget)
|
||||
{
|
||||
IInteractable hitInteractable = hit.collider.GetComponent<IInteractable>();
|
||||
|
||||
// 如果命中对象与之前注视的不一样,触发进入/离开事件
|
||||
if (hitInteractable != previousGazedTarget)
|
||||
{
|
||||
if (previousGazedTarget != null)
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this.gameObject);
|
||||
}
|
||||
|
||||
if (hitInteractable != null)
|
||||
{
|
||||
hitInteractable.OnGazeEnter(this.gameObject);
|
||||
// 这里可以显示交互提示UI,例如 “E - 开门”
|
||||
Debug.Log(hitInteractable.GetInteractPrompt());
|
||||
}
|
||||
|
||||
previousGazedTarget = hitInteractable;
|
||||
}
|
||||
|
||||
currentTarget = hitInteractable;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有命中时,如果之前有注视对象,触发离开
|
||||
if (previousGazedTarget != null)
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this.gameObject);
|
||||
previousGazedTarget = null;
|
||||
}
|
||||
|
||||
currentTarget = null;
|
||||
if (hitInteractable != null)
|
||||
{
|
||||
hitInteractable.OnGazeEnter(this.gameObject);
|
||||
// 这里可以显示交互提示UI,例如 “E - 开门”
|
||||
Debug.Log(hitInteractable.GetInteractPrompt());
|
||||
}
|
||||
|
||||
previousGazedTarget = hitInteractable;
|
||||
}
|
||||
|
||||
currentTarget = hitInteractable;
|
||||
}
|
||||
|
||||
public void SetPlayerInteractionEnabled(bool isEnabled)
|
||||
@@ -92,12 +79,7 @@ namespace Gameplay.Player
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if (isDrawGizmos) return;
|
||||
if (playerCamera == null) return;
|
||||
Gizmos.color = Color.green;
|
||||
Vector3 origin = playerCamera.transform.position;
|
||||
Vector3 direction = playerCamera.transform.forward * interactRange;
|
||||
Gizmos.DrawLine(origin, origin + direction);
|
||||
// 交由 FirstPersonRaycaster 绘制射线
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user