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

which is the best way to reset an variable when OnTriggerExit2D is not executed?

Discussion in '2D' started by pleasurehouse, Mar 18, 2021.

  1. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    I'm using bullet that is destroyed when trigger enter event happens.
    i'm reseting my variable isHurt like this.
    Is there a better way to reset this variable?

    Code (CSharp):
    1.  
    2. int count=2;
    3.  //-------------------------------------------------
    4.     // Hit detector
    5.     //-------------------------------------------------
    6.     private void OnTriggerEnter2D(Collider2D collision)
    7.     {
    8.         if (!state.isHurt && hiters.CheckHiters(collision))
    9.         {
    10.             state.isHurt = true;
    11.             count=0;
    12.         }
    13.     }
    14.     //-------------------------------------------------
    15.     // Reset hit
    16.     //-------------------------------------------------
    17.     private void OnTriggerExit2D(Collider2D collision)
    18.     {
    19.         if (state.isHurt && hiters.CheckHiters(collision))
    20.         {
    21.             state.isHurt = false;  
    22.         }
    23.     }
    24.  
    25.     //-------------------------------------------------
    26.     // Reset hit
    27.     //-------------------------------------------------
    28.     private void Update()
    29.     {
    30.         if (state.isHurt && count == 1)
    31.         {
    32.             state.isHurt = false;
    33.         }
    34.         if (int.MaxValue == count)
    35.         {
    36.             count=2;
    37.         }
    38.         count++;
    39.     }
    40.  
    Thank you so much!!
     
    Last edited: Mar 18, 2021
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Okay, so I'm guessing this is how you want things to work:
    Bullet hits player, player is 'hurt' for a period of time, bullet is destroyed (so never exits).
    Assuming that is the case, what you'd want to do is put in a timer of some kind, and that should use FixedUpdate instead of Update. Update fires every frame, and since that can radically vary, you don't generally want to put gameplay logic there. Update is meant more strictly for your graphical logic. You can use fixeddeltatime to figure out home much time has passed each call to fixedupdate and just keep reducing a 'hurt time' variable by such until it is at or below zero and then you are no longer hurt and can reset such.
     
    pleasurehouse likes this.
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    pleasurehouse likes this.
  4. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96

    Yes!! that is. good idea!! i going to try it. Thank you so much for you help an for you time!! :)
     
  5. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Thank you for your comment. I'm going to try first with the Derekloffin idea. i have read that the coroutines is better avoid it and only use it if absolutely necessary. But it can be another option too.
    Thank you so much for your help.