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

88 lines
3.3 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;
public class PlayerInteractor : MonoBehaviour
{
[Header("Настройки луча")]
[SerializeField] private float interactRange = 3f;
[SerializeField] private LayerMask interactableLayer;
[Header("Клавиши управления")]
[SerializeField] private KeyCode primaryKey = KeyCode.E; // Работа с деталью (открыть/взять)
[SerializeField] private KeyCode secondaryKey = KeyCode.R; // Снять деталь
[SerializeField] private KeyCode enterVehicleKey = KeyCode.F; // Сесть в машину
[Header("Ссылки")]
[SerializeField] private UIManager uiManager;
private void OnDisable()
{
if (uiManager != null) uiManager.HidePrompt();
}
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>();
// Ищем скрипт автомобиля в родительских объектах
VehicleInteraction vehicle = hit.collider.GetComponentInParent<VehicleInteraction>();
if (interactable != null || vehicle != null)
{
// Формируем комбинированную подсказку
string prompt = "";
if (vehicle != null && !vehicle.isPlayerInside)
{
prompt += "[F] Сесть в авто ";
}
if (interactable != null)
{
string partPrompt = interactable.GetPrompt();
if (!string.IsNullOrEmpty(partPrompt))
{
prompt += partPrompt;
}
}
if (!string.IsNullOrEmpty(prompt) && uiManager != null)
{
uiManager.ShowPrompt(prompt);
}
// 1. Посадка в авто на F
if (Input.GetKeyDown(enterVehicleKey) && vehicle != null)
{
vehicle.Interact(transform.root.gameObject);
if (uiManager != null) uiManager.HidePrompt();
return;
}
// 2. Основное действие с деталью на E (Открыть/Закрыть)
if (Input.GetKeyDown(primaryKey) && interactable != null)
{
interactable.Interact(transform.root.gameObject);
}
// 3. Вторичное действие с деталью на R (Снять в руки)
if (Input.GetKeyDown(secondaryKey))
{
ISecondaryInteractable secondary = hit.collider.GetComponentInParent<ISecondaryInteractable>();
secondary?.SecondaryInteract(transform.root.gameObject);
}
return;
}
}
if (uiManager != null)
{
uiManager.HidePrompt();
}
}
}