87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
public class VehicleInteraction : MonoBehaviour, IInteractable
|
|
{
|
|
[Header("Setup")]
|
|
[SerializeField] private Transform exitPoint;
|
|
[SerializeField] private UIManager uiManager;
|
|
|
|
private CarOrbitCamera globalCarCamera; // Ссылка на глобальную камеру
|
|
private CarController carController;
|
|
private GameObject playerInstance;
|
|
private bool isPlayerInside = false;
|
|
|
|
private void Awake()
|
|
{
|
|
carController = GetComponent<CarController>();
|
|
|
|
// Автоматически находим камеру на сцене, даже если она выключена
|
|
globalCarCamera = FindAnyObjectByType<CarOrbitCamera>(FindObjectsInactive.Include);
|
|
}
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
if (!isPlayerInside) EnterVehicle(interactor);
|
|
}
|
|
|
|
public string GetPrompt()
|
|
{
|
|
return "Сесть в машину (E)";
|
|
}
|
|
|
|
private void EnterVehicle(GameObject player)
|
|
{
|
|
isPlayerInside = true;
|
|
playerInstance = player;
|
|
playerInstance.SetActive(false);
|
|
|
|
// Передаем этой машине фокус и включаем камеру
|
|
if (globalCarCamera != null)
|
|
{
|
|
globalCarCamera.SetTarget(transform);
|
|
globalCarCamera.gameObject.SetActive(true);
|
|
}
|
|
|
|
if (uiManager != null) uiManager.ToggleDashboard(true);
|
|
}
|
|
|
|
private void ExitVehicle()
|
|
{
|
|
isPlayerInside = false;
|
|
carController.ProcessInput(0f, 0f, true);
|
|
|
|
// Отвязываем цель и выключаем камеру авто
|
|
if (globalCarCamera != null)
|
|
{
|
|
globalCarCamera.SetTarget(null);
|
|
globalCarCamera.gameObject.SetActive(false);
|
|
}
|
|
|
|
if (uiManager != null) uiManager.ToggleDashboard(false);
|
|
|
|
CharacterController charController = playerInstance.GetComponent<CharacterController>();
|
|
if (charController != null) charController.enabled = false;
|
|
|
|
playerInstance.transform.position = exitPoint.position;
|
|
|
|
if (charController != null) charController.enabled = true;
|
|
|
|
playerInstance.SetActive(true);
|
|
playerInstance = null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isPlayerInside) return;
|
|
|
|
float vertical = Input.GetAxis("Vertical");
|
|
float horizontal = Input.GetAxis("Horizontal");
|
|
bool isBraking = Input.GetKey(KeyCode.Space);
|
|
|
|
carController.ProcessInput(vertical, horizontal, isBraking);
|
|
|
|
if (uiManager != null) uiManager.UpdateSpeed(carController.SpeedKmH);
|
|
|
|
if (Input.GetKeyDown(KeyCode.E)) ExitVehicle();
|
|
}
|
|
} |