refactor(): 使用第一人称射线检测来替换掉 可交互和可编辑种重合的部分
This commit is contained in:
@@ -153,6 +153,7 @@ GameObject:
|
||||
- component: {fileID: 5633281206562703781}
|
||||
- component: {fileID: 1774433545530707224}
|
||||
- component: {fileID: 7145954873497571105}
|
||||
- component: {fileID: 8217262775185484522}
|
||||
m_Layer: 0
|
||||
m_Name: Player
|
||||
m_TagString: Player
|
||||
@@ -255,8 +256,7 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: d7071907d14f433789ea37a8367b584a, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
cardBookData: {fileID: 11400000, guid: 6884501cc489c254aaf6836e84649588, type: 2}
|
||||
playerCardsController: {fileID: 0}
|
||||
MaxHealth: 100
|
||||
--- !u!114 &5633281206562703781
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -287,12 +287,8 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: dc03712b5c254b8380471356f3bac151, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
interactRange: 15
|
||||
interactableLayer:
|
||||
serializedVersion: 2
|
||||
m_Bits: 4294967295
|
||||
playerCamera: {fileID: 4390370389971332335}
|
||||
isDrawGizmos: 0
|
||||
raycaster: {fileID: 0}
|
||||
isEnablePlayerInteraction: 1
|
||||
--- !u!114 &7145954873497571105
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -306,6 +302,24 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
cam: {fileID: 4390370389971332335}
|
||||
--- !u!114 &8217262775185484522
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 3550102669167288263}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 50205a0afd8544b883204613f428e066, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
rayLength: 15
|
||||
layerMask:
|
||||
serializedVersion: 2
|
||||
m_Bits: 4294967295
|
||||
playerCamera: {fileID: 0}
|
||||
drawGizmos: 1
|
||||
--- !u!1 &3610935294554348573
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@@ -5,9 +5,10 @@ using Interface;
|
||||
|
||||
namespace Script.Gameplay.Facility
|
||||
{
|
||||
public class DoorInteractController : InteractableBaseController, IEditableComponent
|
||||
public class DoorInteractController : IInteractable, IEditableComponent
|
||||
{
|
||||
[SerializeField] private bool isActive = true;
|
||||
public bool Interactable = true;
|
||||
public bool IsActive
|
||||
{
|
||||
get => isActive;
|
||||
@@ -22,7 +23,12 @@ namespace Script.Gameplay.Facility
|
||||
public LockLevel LockLevel => LockLevel.Red;
|
||||
private bool isOpened = false;
|
||||
|
||||
public override void Interact(GameObject interactor)
|
||||
public string GetInteractPrompt()
|
||||
{
|
||||
return "按F进行交互";
|
||||
}
|
||||
|
||||
public void Interact(GameObject interactor)
|
||||
{
|
||||
if (!Interactable) return;
|
||||
if (isOpened)
|
||||
@@ -37,6 +43,16 @@ namespace Script.Gameplay.Facility
|
||||
}
|
||||
}
|
||||
|
||||
public void OnGazeEnter(GameObject editor)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void OnGazeExit(GameObject editor)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void OpenDoor()
|
||||
{
|
||||
Debug.Log("Open door");
|
||||
|
72
Assets/Script/Gameplay/Player/FirstPersonRaycaster.cs
Normal file
72
Assets/Script/Gameplay/Player/FirstPersonRaycaster.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Gameplay.Player
|
||||
{
|
||||
/// <summary>
|
||||
/// 通用第一人称射线检测,获取玩家注视的 GameObject。
|
||||
/// </summary>
|
||||
public class FirstPersonRaycaster : MonoBehaviour
|
||||
{
|
||||
[Header("Raycast Settings")]
|
||||
[SerializeField] private float rayLength = 15f;
|
||||
[SerializeField] private LayerMask layerMask = ~0; // 默认检测所有层
|
||||
[SerializeField] private Camera playerCamera;
|
||||
[SerializeField] private bool drawGizmos = true;
|
||||
|
||||
/// <summary>
|
||||
/// 当前玩家注视的 GameObject(只读)。
|
||||
/// </summary>
|
||||
public GameObject CurrentLookAtObject { get; private set; }
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (playerCamera == null)
|
||||
{
|
||||
var camObj = GameObject.FindWithTag("MainCamera");
|
||||
if (camObj != null)
|
||||
playerCamera = camObj.GetComponent<Camera>();
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateLookAtObject();
|
||||
}
|
||||
|
||||
private void UpdateLookAtObject()
|
||||
{
|
||||
if (playerCamera == null)
|
||||
{
|
||||
CurrentLookAtObject = null;
|
||||
return;
|
||||
}
|
||||
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, rayLength, layerMask))
|
||||
{
|
||||
CurrentLookAtObject = hit.collider.gameObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentLookAtObject = null;
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if (!drawGizmos) return;
|
||||
Camera cam = playerCamera;
|
||||
if (cam == null && Application.isPlaying == false)
|
||||
{
|
||||
var camObj = GameObject.FindWithTag("MainCamera");
|
||||
if (camObj != null)
|
||||
cam = camObj.GetComponent<Camera>();
|
||||
}
|
||||
if (cam == null) return;
|
||||
Gizmos.color = Color.cyan;
|
||||
Vector3 origin = cam.transform.position;
|
||||
Vector3 dir = cam.transform.forward * rayLength;
|
||||
Gizmos.DrawLine(origin, origin + dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50205a0afd8544b883204613f428e066
|
||||
timeCreated: 1760775915
|
@@ -7,10 +7,7 @@ namespace Gameplay.Player
|
||||
{
|
||||
public class PlayerEditController:MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float interactRange = 15f;
|
||||
[SerializeField] private LayerMask interactableLayer;
|
||||
[SerializeField] private Camera playerCamera;
|
||||
[SerializeField] private bool isDrawGizmos;
|
||||
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
|
||||
|
||||
private IEditable currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
|
||||
private IEditable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
|
||||
@@ -20,10 +17,14 @@ namespace Gameplay.Player
|
||||
void Start()
|
||||
{
|
||||
inputManager = InputManager.Instance;
|
||||
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.");
|
||||
ControllerLocator.Instance.Register(this);
|
||||
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -33,12 +34,9 @@ namespace Gameplay.Player
|
||||
|
||||
void DetectInteractable()
|
||||
{
|
||||
if (playerCamera == null) return;
|
||||
|
||||
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, interactRange, interactableLayer))
|
||||
{
|
||||
IEditable hitEditable = hit.collider.GetComponent<IEditable>();
|
||||
if (raycaster == null) return;
|
||||
GameObject lookAtObj = raycaster.CurrentLookAtObject;
|
||||
IEditable hitEditable = lookAtObj != null ? lookAtObj.GetComponent<IEditable>() : null;
|
||||
|
||||
// 如果命中对象与之前注视的不一样,触发进入/离开事件
|
||||
if (hitEditable != previousGazedTarget)
|
||||
@@ -58,18 +56,6 @@ namespace Gameplay.Player
|
||||
|
||||
currentTarget = hitEditable;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有命中时,如果之前有注视对象,触发离开
|
||||
if (previousGazedTarget != null)
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this);
|
||||
previousGazedTarget = null;
|
||||
}
|
||||
|
||||
currentTarget = null;
|
||||
}
|
||||
}
|
||||
|
||||
public IEditable GetCurrentTarget()
|
||||
{
|
||||
@@ -78,12 +64,7 @@ namespace Gameplay.Player
|
||||
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
if(!isDrawGizmos) return;
|
||||
if (playerCamera == null) return;
|
||||
Gizmos.color = Color.red;
|
||||
Vector3 origin = playerCamera.transform.position;
|
||||
Vector3 direction = playerCamera.transform.forward * interactRange;
|
||||
Gizmos.DrawLine(origin, origin + direction);
|
||||
// 交由 FirstPersonRaycaster 绘制射线
|
||||
}
|
||||
}
|
||||
}
|
@@ -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,13 +39,11 @@ 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))
|
||||
{
|
||||
IInteractable hitInteractable = hit.collider.GetComponent<IInteractable>();
|
||||
GameObject lookAtObj = raycaster.CurrentLookAtObject;
|
||||
IInteractable hitInteractable = lookAtObj != null ? lookAtObj.GetComponent<IInteractable>() : null;
|
||||
|
||||
// 如果命中对象与之前注视的不一样,触发进入/离开事件
|
||||
if (hitInteractable != previousGazedTarget)
|
||||
@@ -66,18 +65,6 @@ namespace Gameplay.Player
|
||||
|
||||
currentTarget = hitInteractable;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有命中时,如果之前有注视对象,触发离开
|
||||
if (previousGazedTarget != null)
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this.gameObject);
|
||||
previousGazedTarget = null;
|
||||
}
|
||||
|
||||
currentTarget = null;
|
||||
}
|
||||
}
|
||||
|
||||
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