-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckTrigger.cs
More file actions
284 lines (247 loc) · 9.32 KB
/
Copy pathCheckTrigger.cs
File metadata and controls
284 lines (247 loc) · 9.32 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UserInTheBox;
public class CheckTrigger : MonoBehaviour
{
public event System.Action<bool, int> OnButtonTriggered;
public GameObject rightController;
// Buttons and their components
public Button button1;
public Button button2;
public Button button3;
public Button button4; // NEW
public Button button5; // NEW
private Color originalColor1;
private Color originalColor2;
private Color originalColor3;
private Color originalColor4; // NEW
private Color originalColor5; // NEW
public Vector3 buttonPosition;
public Vector3 unhitPosition;
public bool isHit = false;
public bool triggerhit = false;
public bool buttonHit = false;
public bool isCorrectButton = false;
public SimulatedUser user;
public ButtonManager buttonManager;
// Track which button was hit and which is the target
private int lastHitButtonIndex = -1;
public int currentTargetButtonIndex = -1;
public bool timedOutWithoutHit = false;
private void Start()
{
// Store the original colors of all buttons
if (button1 != null)
{
originalColor1 = button1.GetComponent<Image>().color;
}
if (button2 != null)
{
originalColor2 = button2.GetComponent<Image>().color;
}
if (button3 != null)
{
originalColor3 = button3.GetComponent<Image>().color;
}
if (button4 != null) // NEW
{
originalColor4 = button4.GetComponent<Image>().color;
}
if (button5 != null) // NEW
{
originalColor5 = button5.GetComponent<Image>().color;
}
}
private void Update()
{
float elapsed = Time.time - buttonManager.targetActivationtime;
if (!isHit && elapsed > 10f) // time-out
{
timedOutWithoutHit = true;
}
}
private void OnTriggerEnter(Collider other)
{
triggerhit = true;
GameObject hitObject = other.gameObject;
// If sphere child hits, go up to parent (button)
if (hitObject.transform.parent != null)
{
hitObject = hitObject.transform.parent.gameObject;
}
isHit = false;
lastHitButtonIndex = -1;
// ---------------- BUTTON 1–3 (with SimulatedUser support) ----------------
if ((user != null && hitObject == user.button1) ||
(button1 != null && (hitObject == button1.gameObject || hitObject.name == "Button 1")))
{
isHit = true;
lastHitButtonIndex = 0;
buttonPosition = (user != null && user.button1 != null) ? user.button1.transform.position : button1.transform.position;
}
else if ((user != null && hitObject == user.button2) ||
(button2 != null && (hitObject == button2.gameObject || hitObject.name == "Button 2")))
{
isHit = true;
lastHitButtonIndex = 1;
buttonPosition = (user != null && user.button2 != null) ? user.button2.transform.position : button2.transform.position;
}
else if ((user != null && hitObject == user.button3) ||
(button3 != null && (hitObject == button3.gameObject || hitObject.name == "Button 3")))
{
isHit = true;
lastHitButtonIndex = 2;
buttonPosition = (user != null && user.button3 != null) ? user.button3.transform.position : button3.transform.position;
}
// ---------------- NEW: BUTTON 4 & 5 ----------------
else if (button4 != null && (hitObject == button4.gameObject || hitObject.name == "Button 4"))
{
isHit = true;
lastHitButtonIndex = 3;
buttonPosition = button4.transform.position;
}
else if (button5 != null && (hitObject == button5.gameObject || hitObject.name == "Button 5"))
{
isHit = true;
lastHitButtonIndex = 4;
buttonPosition = button5.transform.position;
}
if (isHit)
{
Debug.Log($"*** BUTTON HIT CONFIRMED *** Button {lastHitButtonIndex + 1} at position: {buttonPosition}");
// Compare with ButtonManager's current target (0–4)
isCorrectButton = (lastHitButtonIndex == buttonManager.currentTargetButtonIndex);
Color feedbackColor = isCorrectButton ? Color.red : Color.blue;
ChangeButtonColor(lastHitButtonIndex, feedbackColor);
StartCoroutine(ResetButtonColor(lastHitButtonIndex, 0.2f));
}
else
{
Debug.Log($"*** NO BUTTON MATCH *** Collided with non-button object: {hitObject.name}");
}
// Trigger event after processing
OnButtonTriggered?.Invoke(isHit, lastHitButtonIndex);
}
private void OnTriggerExit(Collider other)
{
GameObject exitObject = other.gameObject;
if (exitObject.transform.parent != null)
{
exitObject = exitObject.transform.parent.gameObject;
}
bool isButtonExit = false;
// Buttons 1–3 via SimulatedUser and local references
if ((user != null && exitObject == user.button1) ||
(user != null && exitObject == user.button2) ||
(user != null && exitObject == user.button3) ||
(button1 != null && exitObject == button1.gameObject) ||
(button2 != null && exitObject == button2.gameObject) ||
(button3 != null && exitObject == button3.gameObject) ||
(button4 != null && exitObject == button4.gameObject) || // NEW
(button5 != null && exitObject == button5.gameObject)) // NEW
{
isButtonExit = true;
Debug.Log("Direct match: Exiting a button");
}
if (isButtonExit)
{
isHit = false;
triggerhit = false;
isCorrectButton = false;
}
else
{
Debug.Log("Exiting non-button object - no state reset");
}
}
private void ChangeButtonColor(int buttonIndex, Color newColor)
{
Button targetButton = GetButton(buttonIndex);
if (targetButton != null)
{
Image buttonImage = targetButton.GetComponent<Image>();
if (buttonImage != null)
{
StartCoroutine(ChangeButtonColorCoroutine(buttonImage, newColor, buttonIndex));
}
}
}
private IEnumerator ChangeButtonColorCoroutine(Image buttonImage, Color newColor, int buttonIndex)
{
Color originalColor = buttonImage.color;
buttonImage.color = newColor;
yield return new WaitForSeconds(0.1f);
buttonImage.color = originalColor;
}
private Button GetButton(int buttonIndex)
{
switch (buttonIndex)
{
case 0: return button1;
case 1: return button2;
case 2: return button3;
case 3: return button4; // NEW
case 4: return button5; // NEW
default:
return button1;
}
}
private Color GetOriginalColor(int buttonIndex)
{
switch (buttonIndex)
{
case 0: return originalColor1;
case 1: return originalColor2;
case 2: return originalColor3;
case 3: return originalColor4; // NEW
case 4: return originalColor5; // NEW
default: return originalColor1;
}
}
// Coroutine to reset the color after a delay
private IEnumerator ResetButtonColor(int buttonIndex, float delay)
{
yield return new WaitForSeconds(delay);
Button targetButton = GetButton(buttonIndex);
if (targetButton != null)
{
Image buttonImage = targetButton.GetComponent<Image>();
if (buttonImage != null)
{
Color originalColor = GetOriginalColor(buttonIndex);
buttonImage.color = originalColor;
}
}
}
// Helper method to highlight the current target button
public void HighlightTargetButton(int targetButtonIndex)
{
currentTargetButtonIndex = targetButtonIndex;
// Reset all buttons to original colors first (now 0–4)
for (int i = 0; i < 5; i++)
{
ChangeButtonColor(i, GetOriginalColor(i));
}
// Highlight the target button with a different color (e.g., yellow)
if (targetButtonIndex >= 0 && targetButtonIndex < 5)
{
ChangeButtonColor(targetButtonIndex, Color.yellow);
}
}
// Helper method to get the last hit button index
public int GetLastHitButtonIndex()
{
return lastHitButtonIndex;
}
// Method to set the current target button (call this from your game logic)
public void SetTargetButton(int targetIndex)
{
currentTargetButtonIndex = targetIndex;
}
// Method to check if the hit was correct
public bool WasLastHitCorrect()
{
return lastHitButtonIndex == currentTargetButtonIndex;
}
}