Enemy Destroy Our Supply!- Game Dev Series 48
Objective: Make our enemies would detect powerups that in front of them and destroy it.
Although the enemies now are some kind of sharpen weapons and almost attacking everything, face to the powerups of Player are still not enough to be feared. If they can even attack and destroy the powerups, that would be interesting.
In this article we will give the ability for enemies to destroy the powerup. This will contain 2 parts:
- Create a detector to enemies then fire if detected.
- Powerup receive the damage that sent by enemies.
To create a detector, I would add a new gameobject under Enemy and named it as PowerupDetector.
Within this gameobject, we can add a rigidbody 2D to it to deal with the collide. Then create a new script “PowerupDetector”.
What we are going to do in this script is quite simple, we detect if any objects collided to it has a tag “Powerup”, if so we can call the Enemy to shoot.
Then back to Enemy and extract some lines from Update() and create a public method “FIreLaser()”.
Then back to editor to add a box collider 2D and adjust the scale.
Now we can set the damage to powerups from enemy lasers.
Destroy the powerups
There are 2 steps to destroy powerups.
- Create a self destroy method to Powerup.
- Add lines of code in Laser to call the method.
Open Powerup and create a new public method.
With this method, we can create an attacking statement in OnTriggerEnter() of Laser script.
And don’t forget to set the tag as “Powerup” to all the powerups.
All done! Now if a powerup was detected by an enemy, it will be destroyed.
Next: Powerups Magnet