D
D
Danil Vaskevich2020-12-27 23:05:56
Unity
Danil Vaskevich, 2020-12-27 23:05:56

How to set up enemy death in Unity 2d?

There is such C# code for Unity 2D

void OnCollisionEnter2D(Collision2D touch)
    {
        

        if (touch.gameObject.tag == "EnemyKill")
        {
            life = life - 1;
            if(life <= 0)
            {
                rgb.AddForce(transform.up * speed, ForceMode2D.Impulse);
                rgb.GetComponent<CapsuleCollider2D>().enabled = false;
            }
        }
    }


When I start testing Unity, the number of lives when colliding with an object with the EnemyKill tag does not decrease. Where could be the error.

PS The variables I need have been created.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SERGEY M, 2020-12-28
@SERGEY09R

Do not reduce the life of the enemy or the player? In the script, as I understand it, only the subtraction of the player's life is written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question