Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I script a timer to countdown faster if a condition is met?

Discussion in 'Scripting' started by visualhippocracy, Apr 27, 2021.

  1. visualhippocracy

    visualhippocracy

    Joined:
    Feb 17, 2020
    Posts:
    6
    hi
    I'm creating a timed game where I have a jumbled up path that the player needs to unjumble to get across. I have created this system and it works
    To this I'm trying to add alternate routes, one route will cause Instant game over, another will cause the timer to go down rapidly and the third will cause the player to teleport to the finish line.
    What would be the best way to go about doing this?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Depends... On how you set up your timer. Show some code and we can probably help you out. Use code tags.
     
  3. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    126
    Since this is a Question often asked i decided to make a small example for my script library. You can find it at my place.
    Just follow my profile link.

     
    visualhippocracy and Kurt-Dekker like this.
  4. visualhippocracy

    visualhippocracy

    Joined:
    Feb 17, 2020
    Posts:
    6
    Hi Sorry for the late replay.
    I've got three scripts working together. Right now I'm working with a prototype using a tutorial I found on youtube about creating a water pipe game
    I'm Pasting them below

    1. Game Manager
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     public Slider slider;
    9.  
    10.     public ParticleSystem particles;
    11.     public GameObject waterSpawner;
    12.     public GameObject levelComplete_Ui;
    13.     public GameObject gameOver_Ui;
    14.     public GameObject nextLevel;
    15.  
    16.     public Animator anim;
    17.  
    18.     public GameObject PipesHolder;
    19.     public GameObject[] Pipes;
    20.  
    21.     [SerializeField]
    22.     int totalPipes = 0;
    23.     [SerializeField]
    24.     int correctedPipes = 0;
    25.  
    26.     float particleSize;
    27.  
    28.     bool isPipeCompleted = false;
    29.     bool isWaterRunning = false;
    30.     bool levelCompleted = false;
    31.     bool gameOver = false;
    32.  
    33.     private float fadeIn_Value = 1.0f;
    34.     private float fadeOut_Value = 1.5f;
    35.     private float value = 0;
    36.  
    37.     void Start()
    38.     {
    39.         totalPipes = PipesHolder.transform.childCount;
    40.  
    41.         Pipes = new GameObject[totalPipes];
    42.        
    43.         for (int i = 0; i < Pipes.Length; i++)
    44.         {
    45.             Pipes[i] = PipesHolder.transform.GetChild(i).gameObject;
    46.         }
    47.  
    48.         isWaterRunning = false;
    49.         Coroutine2(fadeIn_Value);
    50.     }
    51.  
    52.     private void Update()
    53.     {
    54.         if (levelCompleted)
    55.         {
    56.             waterSpawner.GetComponent<WaterSpawner>().Set(false);
    57.             nextLevel.SetActive(true);
    58.             anim.Play("levelComplete");
    59.             levelCompleted = false;
    60.             StopCoroutines();
    61.         }
    62.         if (gameOver)
    63.         {
    64.             gameOver_Ui.SetActive(true);
    65.             gameOver = false;
    66.             anim.Play("gameOver");
    67.             StopCoroutines();
    68.         }
    69.  
    70.         Debug.Log(slider.value);
    71.     }
    72.     public void CorrectMove(GameObject pipe)
    73.     {
    74.         correctedPipes += 1;
    75.         if (correctedPipes == totalPipes)
    76.         {
    77.             Debug.Log("You Win!");
    78.             isPipeCompleted = true;
    79.             isWaterRunning = false;
    80.             levelComplete_Ui.SetActive(true);
    81.             waterSpawner.GetComponent<WaterSpawner>().Set(true);
    82.             waterSpawner.GetComponent<WaterSpawner>().IsOn();
    83.             StopCoroutines();
    84.             Coroutine1(fadeOut_Value);
    85.         }
    86.     }
    87.     public void WrongMove(GameObject pipe)
    88.     {
    89.         waterSpawner.GetComponent<WaterSpawner>().Set(false);
    90.         isPipeCompleted = false;
    91.         correctedPipes -= 1;
    92.         if (!isWaterRunning)
    93.         {
    94.             StopCoroutines();
    95.             Coroutine2(fadeIn_Value);
    96.             isWaterRunning = true;
    97.         }
    98.     }
    99.  
    100.     public void StopCoroutines()
    101.     {
    102.         StopAllCoroutines();
    103.     }
    104.  
    105.     public void Coroutine1(float number)
    106.     {
    107.         value = number;
    108.         StartCoroutine(FadeOut(value));
    109.     }
    110.  
    111.     public void Coroutine2(float number)
    112.     {
    113.         value = number;
    114.        
    115.         StartCoroutine(FadeIn(value));
    116.     }
    117.  
    118.     IEnumerator FadeIn(float value)
    119.     {
    120.         while (!isPipeCompleted && particles.startSize <= 25)
    121.         {
    122.             particleSize += value;
    123.             particles.startSize = particleSize;
    124.  
    125.             if (particles.startSize > 24 || slider.value < 1 && !levelCompleted)
    126.                 gameOver = true;
    127.  
    128.             yield return new WaitForSeconds(1.0f);
    129.         }
    130.     }
    131.  
    132.     IEnumerator FadeOut(float value)
    133.     {
    134.         while (isPipeCompleted && particles.startSize >= 0)
    135.         {
    136.             particles.startSize -= value;
    137.  
    138.             if (particles.startSize == 0)
    139.                 Invoke("levelComplete", 2.0f);
    140.  
    141.             yield return new WaitForSeconds(1f);
    142.         }
    143.     }
    144.  
    145.     public void levelComplete()
    146.     {
    147.         levelCompleted = true;
    148.     }
    149. }
    2. Script for checking pipes rotation

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Pipe : MonoBehaviour
    7. {
    8.     float[] rotations = { 0, 90, 180, 270 };
    9.  
    10.     public float[] correctRotation;
    11.     public bool isPlaced = false;
    12.  
    13.     int PossibleRots = 1;
    14.  
    15.     GameManager gameManager;
    16.  
    17.     private void Awake()
    18.     {
    19.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    20.     }
    21.  
    22.     public bool Get()
    23.     {
    24.         return isPlaced;
    25.     }
    26.  
    27.     public void Set()
    28.     {
    29.         isPlaced = false;
    30.     }
    31.  
    32.  
    33.     private void Start()
    34.     {
    35.         PossibleRots = correctRotation.Length;
    36.         int rand = Random.Range(0, rotations.Length);
    37.         transform.eulerAngles = new Vector3(0, 0, rotations[rand]);
    38.  
    39.         if (PossibleRots > 1)
    40.         {
    41.             if (transform.eulerAngles.z == correctRotation[0] || transform.eulerAngles.z == correctRotation[1])
    42.             {
    43.                 isPlaced = true;
    44.                 gameManager.CorrectMove(this.transform.gameObject);
    45.             }
    46.         }
    47.         else
    48.         {
    49.             if (transform.eulerAngles.z == correctRotation[0])
    50.             {
    51.                 isPlaced = true;
    52.                 gameManager.CorrectMove(this.transform.gameObject);
    53.             }
    54.         }
    55.     }
    56.  
    57.     private void OnMouseDown()
    58.     {
    59.         transform.Rotate(new Vector3(0, 0, 90));
    60.  
    61.         if(gameObject.transform.parent.name == "Pipes")
    62.         {
    63.             if (PossibleRots > 1)
    64.             {
    65.                 if (transform.eulerAngles.z == correctRotation[0] || transform.eulerAngles.z == correctRotation[1] && isPlaced == false)
    66.                 {
    67.                     isPlaced = true;
    68.                     gameManager.CorrectMove(this.transform.gameObject);
    69.                 }
    70.                 else if (isPlaced == true)
    71.                 {
    72.                     isPlaced = false;
    73.                     gameManager.WrongMove(this.transform.gameObject);
    74.                 }
    75.             }
    76.             else
    77.             {
    78.                 if (transform.eulerAngles.z == correctRotation[0] && isPlaced == false)
    79.                 {
    80.                     isPlaced = true;
    81.                     gameManager.CorrectMove(this.transform.gameObject);
    82.                 }
    83.                 else if (isPlaced == true)
    84.                 {
    85.                     isPlaced = false;
    86.                     gameManager.WrongMove(this.transform.gameObject);
    87.                 }
    88.             }
    89.         }
    90.     }
    91. }
    3. Water Tank (Using it as a timer)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class WaterTank : MonoBehaviour
    7. {
    8.     public Slider slider;
    9.  
    10.     public int tankCapacity;
    11.  
    12.     private void Awake()
    13.     {
    14.         SetMaxWater(tankCapacity);
    15.     }
    16.     public void SetMaxWater(int water)
    17.     {
    18.         slider.maxValue = water;
    19.         slider.value = water;
    20.     }
    21.  
    22.     public void SetHealth(int water)
    23.     {
    24.         slider.value = water;
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         slider.value -= Time.deltaTime;
    30.     }
    31.  
    32. }
     
  5. visualhippocracy

    visualhippocracy

    Joined:
    Feb 17, 2020
    Posts:
    6
    thank you! Ill check it out