Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I reset a countdown timer every time I exit out of a collider which sets off the countdown?

Discussion in 'Editor & General Support' started by arjun01bhat, Mar 22, 2023.

  1. arjun01bhat

    arjun01bhat

    Joined:
    Feb 12, 2022
    Posts:
    2
    I am not sure if I have explained this properly but essentially what I want to happen is whenever a rigidbody enters a collider, the timer should go off but should reset itself when the rigidbody is moved out of the collider.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine.UI;
    5. using UnityEngine;
    6.  
    7. public class BreathCheckScript : MonoBehaviour
    8. {
    9.  
    10.     public GameObject TimerCanvas;
    11.  
    12.     //public TMP_Text CountdownText;
    13.  
    14.     //float CurrentTime = 0f;
    15.     //float StartingTime = 10f;
    16.  
    17.     //private void Start()
    18.     //{
    19.  
    20.     //    CurrentTime = StartingTime;
    21.  
    22.     //}
    23.  
    24.     //private void Update()
    25.     //{
    26.  
    27.  
    28.     //    CurrentTime -= 1 * Time.deltaTime;
    29.     //    CountdownText.text = CurrentTime.ToString("0");
    30.  
    31.  
    32.     //    if (CurrentTime <= 0)
    33.     //    {
    34.     //        CurrentTime = 0;
    35.     //    }
    36.     //  //  Debug.Log(CurrentTime);
    37.     //}
    38.  
    39.     private void OnTriggerEnter()
    40.     {
    41.  
    42.         TimerCanvas.SetActive(true);
    43.        
    44.     }
    45.  
    46.     private void OnTriggerExit()
    47.     {
    48.         TimerCanvas.SetActive(false);
    49.  
    50.     }
    51. }
    52.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Steps to success:

    - learn how to use the OnCollisionEnter / OnCollisionExit suite of functions (or implement your own collision test if you prefer, such a with a distance test)

    - set or reset the timer however you like.

    Here's how to do timers:

    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297

    GunHeat (gunheat) spawning shooting rate of fire:

    https://forum.unity.com/threads/spawning-after-amount-of-time-without-spamming.1039618/#post-6729841
     
  3. arjun01bhat

    arjun01bhat

    Joined:
    Feb 12, 2022
    Posts:
    2
    Thanks for your help. Much appreciated.