Search Unity

OnCollisionStay2D is called a few moments after the first contact with other object (Unity 5.4)

Discussion in '2D' started by yasuzo, Aug 22, 2016.

  1. yasuzo

    yasuzo

    Joined:
    Aug 22, 2016
    Posts:
    1
    Recently I updated Unity editor from version 5.3 to 5.4.
    In my game basically you have to find keys and pick the right one for each door.
    And in Unity 5.3 if the player had picked the right key he could just run into the door and the door would open without any delay (like they were unlocked). In Unity 5.4 however (same code, same project) even if the player had chosen the right key and rushed into the door there would be a small delay after he collided with the door before he could open it as if the game lagged.
    Obviously I don't want the delay since it's a runner.
    The function that I used to handle this was OnCollisionStay2D (OnCollisionEnter2D doesn't work for me because I want the player to also have a chance to switch to the right key once he already hit the door).

    Is there anything I can do to fix this?

    Thank you in advance.
     
  2. PeaceLaced

    PeaceLaced

    Joined:
    Jun 2, 2015
    Posts:
    53
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can use OnEnter and OnExit to handle this.

    Use OnEnter to detect the door and call a function to check if the player is holding the right key. If he is, open the door immediately. If not, store a reference to the door in a variable.

    Use OnExit to remove the door from that variable.

    Anytime the player switches keys, call that function to check the player's key against that door variable you have. Just make sure that function checks if the door == null before doing anything with it.