-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuelGauge.cs
88 lines (74 loc) · 2.93 KB
/
FuelGauge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace NWH.VehiclePhysics
{
public class FuelGauge : MonoBehaviour
{
/// <summary>
/// Value at the end of needle travel, at the end angle.
/// </summary>
[Tooltip("Value at the end of needle travel, at the end angle.")]
public float maxValue;
/// <summary>
/// Angle of the needle at the lowest value. You can use lock at start option to adjust this value while in play mode.
/// </summary>
[Tooltip("Angle of the needle at the lowest value. You can use lock at start option to adjust this value while in play mode.")]
public float startAngle = 90f;
/// <summary>
/// Angle of the needle at the highest value. You can use lock at end option to adjust this value while in play mode.
/// </summary>
[Tooltip("Angle of the needle at the highest value. You can use lock at end option to adjust this value while in play mode.")]
public float endAngle = 0f;
/// <summary>
/// Smooths the travel of the needle making it more inert, as if actually had some mass and resistance.
/// </summary>
[Tooltip("Smooths the travel of the needle making it more inert, as if actually had some mass and resistance.")]
[Range(0, 1)]
public float needleSmoothing;
/// <summary>
/// Locks the needle position at the start angle.
/// </summary>
[Tooltip("Locks the needle position at the start angle.")]
public bool lockAtStart = false;
/// <summary>
/// Locks the needle position at the end angle.
/// </summary>
[Tooltip("Locks the needle position at the end angle.")]
public bool lockAtEnd = false;
private GameObject needle;
private float currentValue;
private float angle;
private float prevAngle;
private float percent;
public float Value
{
get
{
return currentValue;
}
set
{
currentValue = Mathf.Clamp(value, 0, maxValue);
}
}
private void Awake()
{
needle = transform.Find("Needle").gameObject;
}
private void Start()
{
angle = startAngle;
}
void Update()
{
percent = Mathf.Clamp01(currentValue / maxValue);
prevAngle = angle;
angle = Mathf.Lerp(startAngle + (endAngle - startAngle) * percent, prevAngle, needleSmoothing);
if (lockAtEnd) angle = endAngle;
if (lockAtStart) angle = startAngle;
needle.transform.eulerAngles = new Vector3(needle.transform.eulerAngles.x, needle.transform.eulerAngles.y, angle);
}
}
}