//We use OnCollisioneEnter for when there is a collision between the two gameObjects/characters
    private void OnCollisionStay2D(Collision2D collision)
    {
        SwordCatController controller = collision.gameObject.GetComponent <SwordCatController>();

        if (controller != null)
        {
            controller.ChangeHealth(-1);
        }
    }
Example #2
0
    private void OnTriggerStay2D(Collider2D collision)

    /*It's enough to write OnTrigger and automatically the compiler will compile the function
     * We use OnTriggerStay for when the character enter and stay in the damage zone to trigger the function
     */
    {
        SwordCatController controller = collision.GetComponent <SwordCatController>();

        if (controller != null)
        {
            controller.ChangeHealth(-1);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        //Debug.Log("Object that entered the trigeer: " + collision);
        SwordCatController controller = collision.GetComponent <SwordCatController>();

        if (controller != null)
        {
            if (controller.health < controller.maxHealth)
            {
                controller.ChangeHealth(1);
                Destroy(gameObject);

                controller.PlaySound(collectedClip);
            }
            else
            {
                MaxHealth();
            }
        }
    }