using BitStrap; using UnityEngine; public class BoatGauge : MonoBehaviour { public enum GaugeType { SPEED = 0, FUEL = 1, DIRECTION = 2 } public GaugeType gaugeType; public Transform pointer; public Vector3 pointerVector = new Vector3(0f, 1f, 0f); public Vector2 pointerMinMax = new Vector2(0f, 180f); [ReadOnly] public float currentValue; public float targetValue; public float speed = 10f; public float speedDown = 10f; private void Start() { Initialize(); } [Button] public void Initialize() { if (gaugeType == GaugeType.DIRECTION) { currentValue = (targetValue = 0.5f); } else if (gaugeType == GaugeType.FUEL) { if (Random.Range(0f, 1f) < 0.5f) { currentValue = (targetValue = Random.Range(0.2f, 0.4f)); } else { currentValue = (targetValue = Random.Range(0.6f, 0.95f)); } } else if (gaugeType == GaugeType.SPEED) { currentValue = (targetValue = 0f); } } private void Update() { currentValue = Mathf.MoveTowards(currentValue, targetValue, ((!(targetValue > currentValue)) ? speedDown : speed) * Time.deltaTime); pointer.localEulerAngles = pointerVector * Mathf.Lerp(pointerMinMax.x, pointerMinMax.y, currentValue); } public void SetValue(float value) { targetValue = value; } }