Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question OnCollisionStay Issue

Discussion in 'Editor & General Support' started by arturnik17, May 28, 2020.

  1. arturnik17

    arturnik17

    Joined:
    Dec 13, 2017
    Posts:
    19
    OnCollisionStay is called every fixed frame (0.02 in my game).
    I have an enemy that follows a player and sticks to him, so first touch is OnCollisionEnter and afterwards OnCollisionStay is calling OnCollisionEnter in it.
    Issue is when multiple enemies are touching the player at the same time OnCollisionStay gets called in every enemy at the exact same frame, so my player is losing more health than it should.

    I have a flag that checks if the player is getting hit but that does not help since all of the OnCollisionStay happen to be called simultaneously. Am I missing something?

    Code (CSharp):
    1.     public virtual void OnCollisionEnter(Collision collision) {
    2.         if (collision.transform.tag == "Collider") {
    3.             if (collision.transform.name == "Player") {
    4.                 // Damage Player
    5.                 if (EnemyManager.playerHit) return; // playerHit is set to true in ReceiveDamage()
    6.                 pController.ReceiveDamage(damage);
    7.                 ChangeDirectionOnHit(collision.GetContact(0).normal.x, collision.GetContact(0).normal.y);
    8.             }
    9.             else {
    10.                 ChangeDirectionOnHitToPlayer();
    11.             }
    12.         }
    13.     }
    OnCollisionStay just calls for OnCollisionEnter.