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

[SOLVED] If no collision for some time do something...

Discussion in 'Scripting' started by ProExCurator, Mar 12, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    How can I write if statement to do something but only if there is no collision with object for some time?
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,469
    1. Whenever there is collision vartime = 0
    2. each cycle vartime = +whatever increment
    3. if vartime > whatever threshold then dosomething™, possibly reset vartime or whatever is needed
     
    ProExCurator likes this.
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    I tried like this:

    Code (CSharp):
    1. ...
    2. private bool keepTiming = true;
    3. private float timer = 3f;
    4. ...
    5. void Update()
    6.     {
    7. ...
    8.        if(keepTiming)
    9.        {
    10.            timer += Time.deltaTime;
    11.        }
    12.        if(timer <= 0)
    13.        {
    14.            collisionCount = 0;
    15.        }
    16. ...
    17. void OnTriggerEnter2D(Collider2D collision)
    18.    {
    19.        if(collision.CompareTag("Enemy"))
    20.        {
    21.            keepTiming = false;
    22.        }
    23. ...
    24.  
    but it doesn't work. Anyone knows why?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You're close... but as @neoshaman said above, actually set the time to zero in your collision.

    Also, start the timer at zero.

    Then ALWAYS count it up in update, regardless of any flags.

    Finally, the only check will be if the timer is above your threshhold, do something.
     
    ProExCurator likes this.
  5. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    I needed to stop it if enemy spawn in room. It's working perfect now ;)

    Code (CSharp):
    1. ...
    2. void Update()
    3.     {
    4. ...
    5. if(keepTiming)
    6.        {
    7.            timer += Time.deltaTime;
    8.            roomTimer.text = "ROOM TIMER: " + Mathf.Round(timer * 100f) / 100f;
    9.        }
    10.        if(timer >= 0.05)
    11.        {
    12.            collisionCount = 0;
    13.            playerWasInRoom = true;
    14.            keepTiming = false;
    15.        }
    16. ...
    17. void OnTriggerEnter2D(Collider2D collision)
    18.    {
    19.        if(collision.CompareTag("Enemy"))
    20.        {
    21.            keepTiming = false;
    22.        }
    23.        if(collision.CompareTag("Player"))
    24.        {
    25.            playerInRoom = true;
    26.            timer = 0;
    27.        }
    28.    }
    29. ...
    Thanks :]
     
    Last edited: Mar 13, 2020
    neoshaman and Kurt-Dekker like this.