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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Trying to count using Time.deltaTime

Discussion in 'Scripting' started by MrSlime443, Mar 25, 2023.

  1. MrSlime443

    MrSlime443

    Joined:
    Mar 12, 2023
    Posts:
    3
    Hey there!

    I've been trying to figure out a way to wait a few seconds before the level reloads (as to let the animation play). I found various people were using
    Code (CSharp):
    1. timer +- Time.deltaTime
    And so I implemented it in a way that I think should work, but isn't?
    Code (CSharp):
    1. public class PlayerDeath : MonoBehaviour
    2. {
    3.     private Animator anim;
    4.     const float waitingTime = 3f;
    5.     private float timer = 0.0f;
    6.     private Rigidbody2D rb;
    7.     CapsuleCollider2D m_Collider;
    8.  
    9.  
    10.      private void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.         rb = GetComponent<Rigidbody2D>();
    14.         m_Collider = GetComponent<CapsuleCollider2D>();
    15.      
    16.     }
    17.  
    18.  
    19.     private void OnCollisionEnter2D(Collision2D collision)
    20.     {
    21.         if (collision.gameObject.CompareTag("Trap"))
    22.         {
    23.             Die();
    24.         }
    25.  
    26.     }
    27.  
    28.     private void Die()
    29.     {
    30.         anim.SetTrigger("Death");
    31.         GetComponent<CharacterMovement>().enabled = false;
    32.         GameObject.Find("Main Camera").GetComponent<FollowPlayer>().enabled = false;
    33.         m_Collider.enabled = false;
    34.         rb.velocity = new Vector3(1, 5, 1);
    35.      
    36.         timer += Time.deltaTime;
    37.         if (timer >= waitingTime)
    38.           SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    39.          
    40.     }
    41. }
    Can any of you tell what I've missed? Thank you so much in advance.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,455
    Oncollision enter only calls once when you enter the collider. So Die doesn't run more than once

    I suggest to use a coroutine and simply use WaitForSeconds as the delay!
     
    Chubzdoomer, MrSlime443 and Homicide like this.
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    656
    As DevDunk suggested, and to get you started...

    Code (CSharp):
    1. private  IEnumerator Die()
    2. {
    3.          if(isDead)yield break;  
    4.  
    5.          isDead = true;
    6.         anim.SetTrigger("Death");
    7.         GetComponent<CharacterMovement>().enabled = false;
    8.         GameObject.Find("Main Camera").GetComponent<FollowPlayer>().enabled = false;
    9.         m_Collider.enabled = false;
    10.         rb.velocity = new Vector3(1, 5, 1);
    11.  
    12.       //  timer += Time.deltaTime;
    13.        // if (timer >= waitingTime)
    14.    
    15.          yield return new WaitForSeconds(waitingTime);
    16.          SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    17.  
    18. }
    Then, simply start the coroutine from your collision method.

    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.gameObject.CompareTag("Trap"))
    4.         {
    5.             StartCoroutine(Die());
    6.         }
    7.     }
     
    DevDunk and MrSlime443 like this.
  4. MrSlime443

    MrSlime443

    Joined:
    Mar 12, 2023
    Posts:
    3

    Thank you! It works perfectly!
     
    Homicide likes this.
  5. MrSlime443

    MrSlime443

    Joined:
    Mar 12, 2023
    Posts:
    3
    Thank you for the suggestion!