Untitled
unknown
plain_text
10 months ago
4.1 kB
4
Indexable
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using DG.Tweening;
public class PlayerVehicle : MonoBehaviour
{
[SerializeField] private DraggyRoad draggyRoad;
[SerializeField] private Slider fuelSlider;
[SerializeField] private Image fuelSliderFill;
[SerializeField] private TextMeshProUGUI fuelText;
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private float maxFuelCapacity = 100f;
[SerializeField] private float fuelConsumptionRate = 1f;
[SerializeField] private float accelerationRate = 2f;
[SerializeField] private float brakingRate = -2f;
private float currentFuel;
private GameObject currentTarget;
private bool isMoving = false;
private bool isBraking = false;
private float currentMoveSpeed = 0f;
private static readonly Color Green = Color.green;
private static readonly Color Yellow = Color.yellow;
private static readonly Color Red = Color.red;
void Start()
{
currentFuel = maxFuelCapacity;
fuelSlider.maxValue = maxFuelCapacity;
fuelSlider.value = currentFuel;
UpdateFuelUI(true);
currentTarget = draggyRoad.GetNextSection();
}
void Update()
{
if (currentFuel <= 0f)
{
fuelText.text = "Empty!";
currentFuel = 0f;
isMoving = false;
return;
}
if (!isMoving)
{
if (currentTarget == null)
{
currentTarget = draggyRoad.GetNextSection();
}
if (currentTarget != null)
{
StartCoroutine(MoveToNextSection());
}
}
currentFuel -= fuelConsumptionRate * Time.deltaTime;
currentFuel = Mathf.Max(currentFuel, 0f);
UpdateFuelUI();
}
IEnumerator MoveToNextSection()
{
isMoving = true;
if (currentTarget != null)
{
Vector3 targetPosition = currentTarget.transform.position;
transform.right = targetPosition - transform.position;
while (Vector3.Distance(transform.position, targetPosition) > 0.1f)
{
if (currentFuel <= 0f)
{
isMoving = false;
yield break;
}
if (isBraking)
{
currentMoveSpeed = Mathf.Max(0f, currentMoveSpeed + (brakingRate * Time.deltaTime);
}else{
currentMoveSpeed = Mathf.Min(moveSpeed, currentMoveSpeed + (accelerationRate * Time.deltaTime));
}
transform.position = Vector3.MoveTowards(transform.position, targetPosition, currentMoveSpeed * Time.deltaTime);
yield return null;
}
transform.position = targetPosition;
}
currentTarget = null;
isMoving = false;
}
private void UpdateFuelUI(bool instant = false)
{
if (instant)
{
fuelSlider.value = currentFuel;
}
else
{
fuelSlider.DOValue(currentFuel, 0.5f);
}
float fuelPercentage = currentFuel / maxFuelCapacity * 100;
fuelText.text = $"{Mathf.RoundToInt(fuelPercentage)}%";
fuelSliderFill.color = GetFuelColor(fuelPercentage);
}
private Color GetFuelColor(float fuelPercentage)
{
if (fuelPercentage > 60f) return Green;
if (fuelPercentage > 30f) return Yellow;
return Red;
}
public void RefillFuel(float amount)
{
currentFuel = Mathf.Clamp(currentFuel + amount, 0f, maxFuelCapacity);
Debug.Log($"Fuel Refilled. Current Fuel: {currentFuel}, Max Capacity: {maxFuelCapacity}");
UpdateFuelUI(true);
}
public void HoldBrake()
{
isBraking = true;
}
public void ReleaseBrake()
{
isBraking = false;
}
}
Editor is loading...
Leave a Comment