kitty
❔ Code is completely skipping over an area
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
using UnityEngine.SceneManagement;
using TMPro;
//const int POPUP_COOLDOWN = 4;
public class Timer : MonoBehaviour
{
bool can_show_popup = true;
float timer = 0.0f;
bool do_timer = false;
public AudioSource playSound;
public string GameOverScene = "GameOverScene";
public Sprite[] sprites; // An array of sprites to change to if the button is not pressed in time
private bool promptActive = false;
public TextMeshProUGUI promptText; // The TextMeshProUGUI object to display the prompt
private int numFailures = 2; // The number of times the button has not been pressed in time
private SpriteRenderer spriteRenderer;
public void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // Get the sprite renderer component
}
void reset_timer()
{
do_timer = false;
timer = 0.0f;
}
IEnumerator do_popup_debounce()
{
can_show_popup = false;
yield return new WaitForSeconds(4);
can_show_popup = true;
}
void Update()
{
if (can_show_popup)
{
int coin_flip = Random.Range(0, 2);
if (coin_flip == 1)
{
//start timer
do_timer = true;
//show popup
ShowPrompt();
StartCoroutine(do_popup_debounce());
}
}
if (do_timer)
{
timer += 1 * Time.deltaTime;
if (Input.GetKey(KeyCode.Space) && timer < 3.9f)
{
//hide popup
Debug.Log("Space hit");
HidePrompt();
reset_timer();
}
else if (Input.GetKey(KeyCode.Space) == false && timer >= 4f)
{
Debug.Log("Recognized button not hit");
numFailures++;
if (numFailures < 2)
{
Debug.Log("Missed one, boat broke");
spriteRenderer.sprite = sprites[numFailures]; // Change the sprite to the corresponding sprite in the array
HidePrompt();
reset_timer();
playSound.Play();
}
else if (numFailures >= 3)
{
SceneManager.LoadScene(GameOverScene); // Load the game over scene
}
}
}
}
void ShowPrompt()
{
promptActive = true;
promptText.gameObject.SetActive(true); // Activate the prompt text object
}
void HidePrompt()
{
promptActive = false;
promptText.gameObject.SetActive(false); // Deactivate the prompt text object
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
using UnityEngine.SceneManagement;
using TMPro;
//const int POPUP_COOLDOWN = 4;
public class Timer : MonoBehaviour
{
bool can_show_popup = true;
float timer = 0.0f;
bool do_timer = false;
public AudioSource playSound;
public string GameOverScene = "GameOverScene";
public Sprite[] sprites; // An array of sprites to change to if the button is not pressed in time
private bool promptActive = false;
public TextMeshProUGUI promptText; // The TextMeshProUGUI object to display the prompt
private int numFailures = 2; // The number of times the button has not been pressed in time
private SpriteRenderer spriteRenderer;
public void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // Get the sprite renderer component
}
void reset_timer()
{
do_timer = false;
timer = 0.0f;
}
IEnumerator do_popup_debounce()
{
can_show_popup = false;
yield return new WaitForSeconds(4);
can_show_popup = true;
}
void Update()
{
if (can_show_popup)
{
int coin_flip = Random.Range(0, 2);
if (coin_flip == 1)
{
//start timer
do_timer = true;
//show popup
ShowPrompt();
StartCoroutine(do_popup_debounce());
}
}
if (do_timer)
{
timer += 1 * Time.deltaTime;
if (Input.GetKey(KeyCode.Space) && timer < 3.9f)
{
//hide popup
Debug.Log("Space hit");
HidePrompt();
reset_timer();
}
else if (Input.GetKey(KeyCode.Space) == false && timer >= 4f)
{
Debug.Log("Recognized button not hit");
numFailures++;
if (numFailures < 2)
{
Debug.Log("Missed one, boat broke");
spriteRenderer.sprite = sprites[numFailures]; // Change the sprite to the corresponding sprite in the array
HidePrompt();
reset_timer();
playSound.Play();
}
else if (numFailures >= 3)
{
SceneManager.LoadScene(GameOverScene); // Load the game over scene
}
}
}
}
void ShowPrompt()
{
promptActive = true;
promptText.gameObject.SetActive(true); // Activate the prompt text object
}
void HidePrompt()
{
promptActive = false;
promptText.gameObject.SetActive(false); // Deactivate the prompt text object
}
}
6 replies