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

61 lines
1.9 KiB
C#

using UnityEngine;
public class PlayerInteractor : MonoBehaviour
{
[SerializeField] private float interactRange = 3f;
[SerializeField] private LayerMask interactableLayer; // Создайте слой "Interactable" в Unity
[SerializeField] private KeyCode primaryKey = KeyCode.E;
[SerializeField] private KeyCode secondaryKey = KeyCode.R;
[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)
{
if (!string.IsNullOrEmpty(interactable.GetPrompt()) && uiManager!=null)
{
uiManager.ShowPrompt(interactable.GetPrompt());
}
else if (uiManager != null)
{
uiManager.HidePrompt();
}
if (Input.GetKeyDown(primaryKey))
{
interactable.Interact(transform.root.gameObject);
}
if (Input.GetKeyDown(secondaryKey))
{
ISecondaryInteractable secondary = hit.collider.GetComponentInParent<ISecondaryInteractable>();
secondary?.SecondaryInteract(transform.root.gameObject);
}
return;
}
}
if (uiManager != null)
{
uiManager.HidePrompt();
}
}
private void OnDisable()
{
if (uiManager != null)
{
uiManager.HidePrompt();
}
}
}