91 lines
2.8 KiB
C#
91 lines
2.8 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;
|
|
[HideInInspector] public bool isPlayerInside;
|
|
|
|
private void Awake()
|
|
{
|
|
carController = GetComponent<CarController>() ?? GetComponentInChildren<CarController>() ?? GetComponentInParent<CarController>();
|
|
if (uiManager == null) uiManager = FindAnyObjectByType<UIManager>();
|
|
globalCarCamera = FindAnyObjectByType<CarOrbitCamera>(FindObjectsInactive.Include);
|
|
}
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
if (!isPlayerInside) EnterVehicle(interactor);
|
|
}
|
|
|
|
public string GetPrompt() => "[F] Сесть в машину";
|
|
|
|
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.HidePrompt();
|
|
uiManager.ToggleDashboard(true);
|
|
}
|
|
}
|
|
|
|
private void ExitVehicle()
|
|
{
|
|
isPlayerInside = false;
|
|
if (carController != null) 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;
|
|
|
|
if (exitPoint != null)
|
|
playerInstance.transform.position = exitPoint.position;
|
|
else
|
|
playerInstance.transform.position = transform.position - transform.right * 1.8f + Vector3.up * 0.5f;
|
|
|
|
if (charController != null) charController.enabled = true;
|
|
|
|
playerInstance.SetActive(true);
|
|
playerInstance = null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isPlayerInside) return;
|
|
|
|
if (carController != null)
|
|
{
|
|
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.F) || Input.GetKeyDown(KeyCode.E))
|
|
ExitVehicle();
|
|
}
|
|
} |