Jump configured

dev-hazim
Hazim 1 month ago
parent b03b3d8daf
commit c82fee9074

@ -97,32 +97,38 @@ public class HurdleManager : MonoBehaviour
} }
} }
void CountTaps()
{
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
currentTapCount++;
Debug.Log($"Tap {currentTapCount}/{requiredTaps}");
}
}
void CheckForJump() void CheckForJump()
{ {
if (currentHurdle == null) return; if (currentHurdle == null || playerRunner.IsJumping()) return;
float remainingDistance = GetRemainingDistance(playerRunner.transform.position, currentHurdle.position); float remainingDistance = GetRemainingDistance(playerRunner.transform.position, currentHurdle.position);
// When very close to hurdle // When very close to hurdle and challenge is completed
if (remainingDistance < 1f && challengeCompleted) if (remainingDistance < 5f && challengeCompleted)
{ {
// Trigger jump // Trigger jump in the PlayerPathRunner
TriggerJump(); playerRunner.TriggerJump(() => {
// This callback runs after jump completes
// Remove hurdle and end challenge after jump if (currentHurdle != null)
hurdles.Remove(currentHurdle); {
if (activeChallenge != null) hurdles.Remove(currentHurdle);
StopCoroutine(activeChallenge); if (activeChallenge != null)
EndChallengeUI(); StopCoroutine(activeChallenge);
EndChallengeUI();
}
});
} }
} }
void TriggerJump()
{
Debug.Log("🦘 JUMP! Player successfully jumped over the hurdle!");
GetComponentInChildren<Animator>().SetTrigger("Jump");
}
void EndChallengeUI() void EndChallengeUI()
{ {
challengeRunning = false; challengeRunning = false;
@ -132,21 +138,12 @@ public class HurdleManager : MonoBehaviour
activeChallenge = null; activeChallenge = null;
} }
void CountTaps()
{
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
currentTapCount++;
Debug.Log($"Tap {currentTapCount}/{requiredTaps}");
}
}
void OnTriggerEnter(Collider other) void OnTriggerEnter(Collider other)
{ {
if (other.CompareTag("Hurdle")) if (other.CompareTag("Hurdle"))
{ {
// Check if this is the current hurdle and challenge wasn't completed // Check if this is the current hurdle and challenge wasn't completed
if (other.transform == currentHurdle && !challengeCompleted) if (other.transform == currentHurdle && !challengeCompleted && !playerRunner.IsJumping())
{ {
Debug.Log("❌ GAME OVER! Failed to complete tap challenge!"); Debug.Log("❌ GAME OVER! Failed to complete tap challenge!");
GameOver(); GameOver();
@ -173,7 +170,7 @@ public class HurdleManager : MonoBehaviour
tapsRequiredText.text = "GAME OVER!"; tapsRequiredText.text = "GAME OVER!";
Debug.Log("💀 GAME OVER - Player failed to clear hurdle!"); Debug.Log("💀 GAME OVER - Player failed to clear hurdle!");
GetComponentInChildren<Animator>().enabled = false;
// You can add more game over logic here: // You can add more game over logic here:
// - Show game over screen // - Show game over screen
// - Play game over sound // - Play game over sound
@ -240,6 +237,4 @@ public class HurdleManager : MonoBehaviour
float t = Mathf.Clamp01(Vector3.Dot(point - a, ab) / Vector3.Dot(ab, ab)); float t = Mathf.Clamp01(Vector3.Dot(point - a, ab) / Vector3.Dot(ab, ab));
return a + t * ab; return a + t * ab;
} }
// Removed the old IsBetween method as it's no longer needed
} }

@ -16,6 +16,11 @@ public class PlayerPathRunner : MonoBehaviour
public bool useTapToMove = true; public bool useTapToMove = true;
public float tapWindowSeconds = 0.5f; public float tapWindowSeconds = 0.5f;
[Header("Jump Settings")]
public float jumpPower = 2f;
public float jumpDuration = 0.5f;
public float jumpDistance = 3f; // Distance in world units
[Header("Path Setup")] [Header("Path Setup")]
public DOTweenPath pathSource; public DOTweenPath pathSource;
@ -27,6 +32,8 @@ public class PlayerPathRunner : MonoBehaviour
private float pathPosition = 0f; private float pathPosition = 0f;
private Vector3[] drawPoints; private Vector3[] drawPoints;
private Path bakedPath; private Path bakedPath;
private bool isJumping = false;
private System.Action onJumpComplete;
private void Awake() private void Awake()
{ {
@ -64,6 +71,9 @@ public class PlayerPathRunner : MonoBehaviour
void Update() void Update()
{ {
// Don't process normal movement if jumping
if (isJumping) return;
if (useTapToMove) if (useTapToMove)
{ {
HandleInput(); HandleInput();
@ -104,7 +114,12 @@ public class PlayerPathRunner : MonoBehaviour
pathPosition += speed * Time.deltaTime; pathPosition += speed * Time.deltaTime;
pathPosition %= 1f; pathPosition %= 1f;
float floatIndex = pathPosition * (drawPoints.Length - 1); UpdatePositionOnPath(pathPosition);
}
void UpdatePositionOnPath(float normalizedPosition)
{
float floatIndex = normalizedPosition * (drawPoints.Length - 1);
int iA = Mathf.FloorToInt(floatIndex); int iA = Mathf.FloorToInt(floatIndex);
int iB = Mathf.Min(iA + 1, drawPoints.Length - 1); int iB = Mathf.Min(iA + 1, drawPoints.Length - 1);
float t = floatIndex - iA; float t = floatIndex - iA;
@ -113,8 +128,67 @@ public class PlayerPathRunner : MonoBehaviour
transform.position = pos; transform.position = pos;
} }
public void TriggerJump(System.Action onComplete = null)
{
if (isJumping) return;
Debug.Log("🦘 Jump triggered in PlayerPathRunner!");
isJumping = true;
onJumpComplete = onComplete;
// Calculate how much to advance on the path based on world distance
float pathLength = CalculateTotalPathLength();
float jumpPercentage = jumpDistance / pathLength;
// Calculate end position on path
float targetPathPosition = pathPosition + jumpPercentage;
targetPathPosition %= 1f; // Wrap around if needed
// Get world positions for start and end
Vector3 startPos = transform.position;
Vector3 endPos = GetWorldPositionAtPathPosition(targetPathPosition);
// Do the jump
transform.DOJump(endPos, jumpPower, 1, jumpDuration)
.SetEase(Ease.OutQuad)
.OnComplete(() => {
// Update our path position to match where we jumped to
pathPosition = targetPathPosition;
isJumping = false;
Debug.Log("Jump completed!");
onJumpComplete?.Invoke();
onJumpComplete = null;
});
}
float CalculateTotalPathLength()
{
float totalLength = 0f;
for (int i = 0; i < drawPoints.Length - 1; i++)
{
totalLength += Vector3.Distance(drawPoints[i], drawPoints[i + 1]);
}
return totalLength;
}
Vector3 GetWorldPositionAtPathPosition(float normalizedPos)
{
float floatIndex = normalizedPos * (drawPoints.Length - 1);
int iA = Mathf.FloorToInt(floatIndex);
int iB = Mathf.Min(iA + 1, drawPoints.Length - 1);
float t = floatIndex - iA;
return Vector3.Lerp(drawPoints[iA], drawPoints[iB], t);
}
public float GetCurrentSpeed() public float GetCurrentSpeed()
{ {
return currentSpeed; return currentSpeed;
} }
public bool IsJumping()
{
return isJumping;
}
} }
Loading…
Cancel
Save