예제 #1
0
        private void tryFireBullet()
        {
            Enemy[] enemies            = FindObjectsOfType <Enemy>();
            float   minDistanceSquared = range * range;
            Enemy   nearestEnemy       = null;

            foreach (Enemy enemy in enemies)
            {
                float distanceSquared = (enemy.gameObject.transform.position - gameObject.transform.position).sqrMagnitude;
                if (distanceSquared < minDistanceSquared)
                {
                    nearestEnemy       = enemy;
                    minDistanceSquared = distanceSquared;
                }
            }

            if (nearestEnemy == null)
            {
                return;
            }

            TowerBullet bullet = Instantiate(bulletPrefab);

            bullet.transform.position = gameObject.transform.position;
            bullet.target             = nearestEnemy.gameObject.transform.position;
            bullet.source             = this;
        }
예제 #2
0
파일: Enemy.cs 프로젝트: njelly/ludumdare44
        public void OnTriggerEnter2D(Collider2D col)
        {
            TowerBullet bullet = col.gameObject.GetComponent <TowerBullet>();

            if (bullet == null)
            {
                return;
            }

            health -= 1;
            if (health <= 0)
            {
                Destroy(gameObject);
                bullet.source.killCount++;

                GameController.Score += 1;
            }

            Destroy(bullet.gameObject);
        }