Perekyp/Assets/Scripts/Player/Interactor/PlayerInteractor.cs

36 lines
1.1 KiB
C#

using UnityEngine;
public class PlayerInteractor : MonoBehaviour
{
[SerializeField] private float interactRange = 3f;
[SerializeField] private LayerMask interactableLayer; // Создайте слой "Interactable" в Unity
[Header("References")]
[SerializeField] private UIManager uiManager;
private void Update()
{
// Пускаем луч из центра экрана
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, interactRange, interactableLayer, QueryTriggerInteraction.Collide))
{
IInteractable interactable = hit.collider.GetComponentInParent<IInteractable>();
if (interactable != null)
{
uiManager.ShowPrompt(interactable.GetPrompt());
if (Input.GetKeyDown(KeyCode.E))
{
interactable.Interact(transform.root.gameObject);
uiManager.HidePrompt();
}
return;
}
}
uiManager.HidePrompt();
}
}