You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ReactRacetrack/Assets/Scripts/PlayerHurdleChecker.cs

78 lines
1.9 KiB
C#

1 month ago
using UnityEngine;
using System.Collections;
public class PlayerHurdleChecker : MonoBehaviour
{
public float speed = 2f;
public float detectionDistance = 10f;
public LayerMask hurdleLayer;
public int requiredTaps = 5;
private Transform nextHurdle;
private float timeToReachHurdle;
private bool awaitingTaps = false;
private int tapCount = 0;
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
if (!awaitingTaps)
DetectNextHurdle();
else
CheckForTaps();
}
void DetectNextHurdle()
{
if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, detectionDistance, hurdleLayer))
{
nextHurdle = hit.transform;
float distance = hit.distance;
timeToReachHurdle = distance / speed;
StartCoroutine(BeginTapChallenge(timeToReachHurdle));
}
}
IEnumerator BeginTapChallenge(float duration)
{
awaitingTaps = true;
tapCount = 0;
Debug.Log($"Tap {requiredTaps} times in {duration:F2} seconds!");
float timer = 0f;
while (timer < duration)
{
timer += Time.deltaTime;
yield return null;
if (tapCount >= requiredTaps)
{
JumpOverHurdle();
yield break;
}
}
FailChallenge();
}
void CheckForTaps()
{
if (Input.GetMouseButtonDown(0) || Input.touchCount > 0)
tapCount++;
}
void JumpOverHurdle()
{
Debug.Log("Success! Jumped over hurdle!");
awaitingTaps = false;
// Optional: Play jump animation or tween upward
}
void FailChallenge()
{
Debug.Log("Failed to jump. Game Over.");
awaitingTaps = false;
// Trigger game over logic here
}
}