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.

Powerups detector

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.

Don’t forget to set as trigger.

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.

You can also create an animation prefab to it.

--

--

--

A passionate gamer whose goal is to work in video game development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Yet Another Blog on Installing CUDA 10.2 on Ubuntu 18.04 MSI Laptop

Supercharging Government Organizations using Technology

Kind of a Newsletter: Changing Faces

Leet Code: Two City Scheduling

Working with Hashicorp Vault API’s

WSO2's Data Sync Tool: Making Migration Seamless

Develop a Machine Learning-based App with Python

Docker Container

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

Converting from 3D to 2D in Unity

Create A White Noise Shader With Shader Graph In Unity

Day 105: Working with Animation Events in Unity

What is a Control Track in Timeline