Add enemies that can destroy pickups

Jaime
Jun 10, 2021

--

The next requirement is to get the enemy to destroy pickups if these are in front of them. Just like detecting the player in the previous articles, we will need to detect the Power ups too.

To damage the Power up, we need to add tag to the Powerup so the laser can determine if it hit it or not.

On Enemy’s OnTriggerEnter2D method, it detects if there’s a Collider with a PowerupPresence tag and if it can fire, shoot it down:

if (other.CompareTag("PowerupPresence"))
{
if (Time.time > _canFire)
{
_fireRate = Random.Range(3f, 7f);
_canFire = Time.time + _fireRate;
Instantiate(_laserFrontPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
}
}

On the Laser’s OnTriggerEntry2D, destroy the Power up if the collider tag is Powerup.

if (other.CompareTag("Powerup") && _isEnemyLaser)
{
Debug.Log("Enemy Laser hit Powerup");
Destroy(other.gameObject);
gameObject.GetComponent<SpriteRenderer>().enabled = false;
}

Now, the enemy can destroy power ups.

--

--

Jaime
Jaime

No responses yet