Files
2025TapTapGameJam/Assets/Script/Gameplay/Player/PlayerInteractorController.cs
2025-10-15 21:31:13 +08:00

58 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using Share;
using System;
using Input;
namespace Gameplay.Player
{
public class PlayerInteractorController : MonoBehaviour
{
[SerializeField] private float interactRange = 15f;
[SerializeField] private LayerMask interactableLayer;
[SerializeField] private Camera playerCamera;
private IInteractable currentTarget;
void Start()
{
playerCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
}
void Update()
{
DetectInteractable();
if (currentTarget != null && InputManager.Instance.InteractPressed)
{
currentTarget.Interact(this.gameObject);
}
}
void DetectInteractable()
{
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, interactRange, interactableLayer))
{
currentTarget = hit.collider.GetComponent<IInteractable>();
if (currentTarget != null)
{
// 这里可以显示交互提示UI例如 “E - 开门”
Debug.Log(currentTarget.GetInteractPrompt());
}
}
else
{
currentTarget = null;
}
}
void OnDrawGizmos()
{
if (playerCamera == null) return;
Gizmos.color = Color.green;
Vector3 origin = playerCamera.transform.position;
Vector3 direction = playerCamera.transform.forward * interactRange;
Gizmos.DrawLine(origin, origin + direction);
}
}
}