Example #1
0
    void OnCollisionEnter2D(Collision2D col)
    {
        BreakableShield shield = col.gameObject.GetComponent <BreakableShield>();

        if (shield)
        {
            // -- don't let it do damage as it's falling from a shield
            canDoDamage = false;
        }
    }
    void CrosshairReleaseAction(Collider2D[] colliders)
    {
        // -- array of enemies that is in overlap circle of crosshair
        //List<Enemy> enemies = new List<Enemy>();

        CrosshairObject firstObjectFound = null;

        for (int i = 0; i < colliders.Length; i++)
        {
            CrosshairObject crossObject = colliders[i].gameObject.GetComponent <CrosshairObject>();

            // -- if it is a crosshair object
            if (crossObject)
            {
                BreakableShield shield = crossObject.GetComponent <BreakableShield>();
                if (shield)
                {
                    // -- override first object with shield then break loop
                    firstObjectFound = shield;
                    break;
                }
            }

            // -- if you haven't got a first object set it to this one
            if (!firstObjectFound)
            {
                firstObjectFound = crossObject;
            }
        }

        if (firstObjectFound)
        {
            // -- if it is targetable and it's not already marked and you have enough shots left
            if (firstObjectFound.isTargetable() && !firstObjectFound.isMarked() && shotsLeft > 0)
            {
                StartCoroutine(ShootAfterDelay(firstObjectFound));
                shotsLeft -= 1;
                bulletCounter.SetBulletNumber(shotsLeft);
            }
        }


        // -- if shots are now zero then start timer
        if (shotsLeft == 0)
        {
            timer.StartTimer(reloadTime);
        }

        // -- move it back to start position
        crosshair.transform.position = crosshairStartPos;
    }