-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCheckpointsAndLaps.cs
146 lines (128 loc) · 4.54 KB
/
CheckpointsAndLaps.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckpointsAndLaps : MonoBehaviour
{
[Header("Points")]
public GameObject start;
public GameObject end;
public GameObject[] checkpoints;
[Header("Settings")]
public float laps = 1;
[Header("Information")]
private float currentCheckpoint;
private float currentLap;
private bool started;
private bool finished;
private float currentLapTime;
private float bestLapTime;
private float bestLap;
private void Start()
{
currentCheckpoint = 0;
currentLap = 1;
started = false;
finished = false;
currentLapTime = 0;
bestLapTime = 0;
bestLap = 0;
}
private void Update()
{
if (started && !finished)
{
currentLapTime += Time.deltaTime;
if (bestLap == 0)
{
bestLap = 1;
}
}
if (started)
{
if (bestLap == currentLap)
{
bestLapTime = currentLapTime;
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Checkpoint"))
{
GameObject thisCheckpoint = other.gameObject;
// Started the race
if (thisCheckpoint == start && !started)
{
print("Started");
started = true;
}
// Ended the lap or race
else if (thisCheckpoint == end && started)
{
// If all the laps are finished, end the race
if (currentLap == laps)
{
if (currentCheckpoint == checkpoints.Length)
{
if (currentLapTime < bestLapTime)
{
bestLap = currentLap;
}
finished = true;
print("Finished");
}
else
{
print("Did not go through all checkpoints");
}
}
// If all laps are not finished, start a new lap
else if (currentLap < laps)
{
if (currentCheckpoint == checkpoints.Length)
{
if (currentLapTime < bestLapTime)
{
bestLap = currentLap;
bestLapTime = currentLapTime; // Because the update function has already run this frame, we need to add this line or it won't work
}
currentLap++;
currentCheckpoint = 0;
currentLapTime = 0;
print($"Started lap {currentLap}");
}
else
{
print("Did not go through all checkpoints");
}
}
}
// Loop through the checkpoints to compare and check which one the player touched
for (int i = 0; i < checkpoints.Length; i++)
{
if (finished)
return;
// If the checkpoint is correct
if (thisCheckpoint == checkpoints[i] && i + 1 == currentCheckpoint + 1)
{
print($"Correct Checkpoint: {Mathf.FloorToInt(currentLapTime / 60)}:{currentLapTime % 60:00.000}");
currentCheckpoint++;
}
// If the checkpoint is incorrect
else if (thisCheckpoint == checkpoints[i] && i + 1 != currentCheckpoint + 1)
{
print($"Incorrect checkpoint");
}
}
}
}
private void OnGUI()
{
// Current time
string formattedCurrentLapTime = $"Current: {Mathf.FloorToInt(currentLapTime / 60)}:{currentLapTime % 60:00.000} - (Lap {currentLap})";
GUI.Label(new Rect(50, 10, 250, 100), formattedCurrentLapTime);
// Best time
string formattedBestLapTime = $"Best: {Mathf.FloorToInt(bestLapTime / 60)}:{bestLapTime % 60:00.000} - (Lap {bestLap})";
GUI.Label(new Rect(250, 10, 250, 100), (started) ? formattedBestLapTime : "0:00.000");
}
}