Example #1
0
 bool EnemyIsAlive()
 {
     searchCountdown -= Time.deltaTime;
     if (searchCountdown <= 0f)
     {
         searchCountdown = 1f;
         GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
         if (enemies.Length == 0)
         {
             return(false);
         }
         else if (enemies.Length < 7 && state == SpawnState.WAITING && !autoTargetting)
         {
             // the last few enemies auto target players
             autoTargetting = true;
             print("Auto  targeting");
             GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
             for (int i = 0; i < enemies.Length; i++)
             {
                 ZombieAI z = enemies[i].GetComponent <ZombieAI>();
                 z.SetTarget(players[Random.Range(0, players.Length)].transform);
             }
         }
     }
     return(true);
 }
Example #2
0
    protected override void attack(string attacker)
    {
        // play sound
        GM.PlayAudio(attackSound);
        // Show muzzle flash
        StartCoroutine(showMuzzleFlash());

        // Raycast bullet
        Vector2      firePos = firePoint.position;
        Vector2      dir     = (firePos - (Vector2)transform.position).normalized;
        RaycastHit2D hit     = Physics2D.Raycast(firePos, dir, range, whatToHit);
        Vector3      normal  = fakeNormal;
        Vector3      hitPos  = (dir * (range / 1)) + firePos;

        if (hit.collider)
        {
            normal = hit.normal;
            hitPos = hit.point;
            Debug.DrawLine(firePos, hit.point, Color.red);
            GameCharacter c = hit.collider.GetComponent <GameCharacter>();
            if (c && (c.tag != "Player" || GM.instance.friendlyFire))
            {
                c.Hurt(attackDamage, attacker);
                c.KnockBack(dir * attackKnockBack);
                ZombieAI ai = c.GetComponent <ZombieAI>();
                if (ai)
                {
                    ai.SetTarget(transform);
                }
            }
        }

        showEffect(hitPos, normal);
    }