feat():大改前的提交,这个版本保存了所有冗余的代码
This commit is contained in:
86
Assets/Script/Gameplay/Player/PlayerEditController.cs
Normal file
86
Assets/Script/Gameplay/Player/PlayerEditController.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using UnityEngine;
|
||||
using Interface;
|
||||
using Script.Gameplay.Input;
|
||||
|
||||
namespace Gameplay.Player
|
||||
{
|
||||
public class PlayerEditController:MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float interactRange = 15f;
|
||||
[SerializeField] private LayerMask interactableLayer;
|
||||
[SerializeField] private Camera playerCamera;
|
||||
[SerializeField] private bool isDrawGizmos;
|
||||
|
||||
private IEditable currentTarget; // 射线命中的当前可编辑对象(用于按键交互)
|
||||
private IEditable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
|
||||
|
||||
private InputManager inputManager;
|
||||
|
||||
void Start()
|
||||
{
|
||||
inputManager = InputManager.Instance;
|
||||
if (playerCamera == null)
|
||||
playerCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
|
||||
|
||||
inputManager.Input.Player.SwitchWatchMode.performed += ctx =>
|
||||
{
|
||||
if (currentTarget != null) currentTarget.BeginEdit();
|
||||
};
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
DetectInteractable();
|
||||
}
|
||||
|
||||
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 (hitEditable != previousGazedTarget)
|
||||
{
|
||||
if (previousGazedTarget != null)
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this);
|
||||
}
|
||||
|
||||
if (hitEditable != null)
|
||||
{
|
||||
hitEditable.OnGazeEnter(this);
|
||||
}
|
||||
|
||||
previousGazedTarget = hitEditable;
|
||||
}
|
||||
|
||||
currentTarget = hitEditable;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有命中时,如果之前有注视对象,触发离开
|
||||
if (previousGazedTarget != null)
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this);
|
||||
previousGazedTarget = null;
|
||||
}
|
||||
|
||||
currentTarget = null;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user