Hazard collision takes two lives instead of one

I have noticed that when my player dies by touching hazards he losses two lives instead of one.
Since this does not happen when he dies by touching an enemy, and since it doesn’t happen when I make sure to file in one hazard tile, I concluded this is because he is falling on two hazard tiles at the same time. (I’m using spikes so far, no water.)

I don’t know how to set it in code so that he loses only one live regardless of how many hazards he collides with. Please help.

I’m trying to understand what you mean. Are you placing to spike tiles together and the player falls in between them and this is what is causing the problem?

It’s a similar issue with the coins. Because there are two colliders that are being triggered, the body and the feet, this can result in multiple times being triggered. It must be the tiny amount of time that it takes to process the script that allows it to register twice before destroying it. I’ve added a check for the coins, I’ll look for a way to do it on the player also. Perhaps by setting the ProcessPlayerDeath() method to only happen if the player is alive. Something like this:

private void Die()
{
    if (myBodyCollider2D.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards")))
    {
        myAnimator.SetTrigger("Dying");
        GetComponent<Rigidbody2D>().velocity = deathKick;
        if (isAlive) { FindObjectOfType<GameSession>().ProcessPlayerDeath(); }
        isAlive = false;
    }
}
private void OnTriggerEnter2D(Collider2D collision)
{
    if (!addedToScore)
    {
        FindObjectOfType<GameSession>().AddToScore(pointsToAdd);
        AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position);
        Destroy(gameObject);
        addedToScore = true;
    }
}
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms