Bomber Enemy

In this article we are going to cover creating an Enemy that will have the following behavior:

  1. Main Ship will bounce back and forth.
  2. The Main Ship will have 4 Drone Bombers rotating around the Main Ship.
  3. The Drone Bombers will sporadically drop bombs that travel down the screen.
  4. The bombs will explode if it hits the Player or after a random time.
  5. If the bomb doesn’t hit the player it will self explode after a random time.
  6. If the Main Ship is hit it will destroy all of the drone with it.

Main Ship Movement

Declared Variables

For this movement I decided to try something different and use Coroutines to control when the Enemy should change directions. We are going to use a bool to keep of when we should move left or right.

In the Update, we use an IF ELSE statement that will check if that moving left bool is true or false. If it is true will translate the enemy to move left using Vector3.left and is the bool is false we will use Vector3.right. In each of these statements we start a coroutine that switch the bool to either true or false.

Coroutines

After a set amount of seconds we altered the bool to switch the movement of the Main Ship movement.

Drone Bombers Movement

The Drone Bomber will rotate around the Main Ship.

Main Ship is Parent of Drone Bomber

The Drone Bomber is a child of the Main Ship, so will move with it.

Declared Variables

We are going to declare float that will control the orbiting and a GameObject that will hold the main ship, so we can access its position. Both have SerializeField attributes, which it possible to assign and adjust in the Inspector.

Assigning the Main Ship Game Object in the Inspector

In Update, we want to use transform.RotateAround. This requires a position you want to rotate around, the direction you want to rotate and the speed.

We are rotating around the Main Ship (_mainShip.transform.position), the direction is Vector3.back (Z-Axis), and speed (_orbitspeed * time.deltaTime).

This is going to rotate the Drone Bomber around the Main Ship in a counter-wise direction from its initial position.

Drone Bombers Dropping Bombs

I am not going to go into detail about the Drone Bombers dropping bombs because I have covered this in previous articles when creating an Enemy Fire method.

EnemyFire method

We are going to Instantiate a Bomb Prefab, which will have its own script, so make sure to declare a GameObject with a SerializedField attribute, so we can assign the bomb prefab in the Inspector.

Bomb Script

In the OnStart, we will start the Explosion Count Down, which will want between 2 to 6 seconds before activating the explosion animation and destroying itself, unless it hits the player before that time.

In the Update, we are translating the bomb to move down from wherever it was instantiate from the Drone Bomber script.

Bomb Script

On Main Ship Destroyed

When the Main Ship is destroyed I wanted to have the remaining Drone Bombers to activate the explosion animation along with the Main Ship. Right now, when the Main Ship is destroyed the Drone Ships just disappear since Child GameObjects are destroyed with the Parent GameObject.

The Drone Bombers need to know when the Main Ship is destroyed, so we will create a public bool in the Main Ship script and set it to false.

When a Player Projectile Hits the Main Ship
On Death method

When the Main Ship is destroyed it will call this public meth that will set this bool to true, which will notify the remain Drone Bombers it has been destroyed and they need to trigger their explosion animations.

Checking to See if the Main Ship is Destroyed

Since we already have access to the Main Ship when we got its position to rotate around, we just need access to its script and that public bool. In the Update, we are checking to see if the mainShipDestroyed bool is true and when it is, we will trigger the explosion animation.

Conclusion

I set out to just create an enemy with a shield, but ended up creating a much more complicated enemy instead. There are a lot of tiny aspects in this enemy that hopefully you find useful.

--

--

--

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

Recommended from Medium

Cross AWS Account Pull ECR image

Dropbox zipfile with csv files to Pandas

How to Build Your First Schema on Flamelink, a Headless CMS for Firebase?

Working with Junior Developers

Azure SQL Database AdventureWorks to Databricks Delta Migration

Day 9 of 30 Days of Code with Python (HackerRank)

DIGITALAX Whitepaper V3 Release

Python Turtle Graphics Tutorial for Beginners

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
Ryan McCoach

Ryan McCoach

More from Medium

Cinematography in Unity — Fade to Black

Post Processing Primer for Unity

Unity Physics

Update: UI Panels in Unity