M
M
Makasin2285782020-12-25 22:53:01
Unity
Makasin228578, 2020-12-25 22:53:01

How to add a collision delay?

There are obstacles, an enemy and ladders, but with 1 collision, several health is removed at once. How can this be fixed?

Bot collision check code:

void OnCollisionEnter2D(Collision2D collision)
    {
        if (health <= 0)
        {
            ReloadLevel();
        }

        if (collision.gameObject.tag == "enemy")
        {
            anim.SetTrigger("Hit");
            health--;
        }
    }

Код проверки столкновения с трапой: 

   void OnTriggerEnter2D(Collider2D collder)
    {
        if (health <= 0)
        {
            ReloadLevel();
        }
        
        if (collder.gameObject.tag == "saw")
        {
             health--;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2020-12-26
@ShockWave2048

bool isDamaged = false;
void OnTriggerEnter2D(Collider2D collder)
    {
        if (isDamaged) return;
        
        if (collder.gameObject.tag == "saw")
        {
             health--;
             isDamaged = true;
             Invoke("ClearDamaged", 2f);
        }
    }

void ClearDamaged()
{
    isDamaged = false;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question