In this article we are going to cover creating an Enemy that will have the following behavior:
- Main Ship will bounce back and forth.
- The Main Ship will have 4 Drone Bombers rotating around the Main Ship.
- The Drone Bombers will sporadically drop bombs that travel down the screen.
- The bombs will explode if it hits the Player or after a random time.
- If the bomb doesn’t hit the player it will self explode after a random time.
- If the Main Ship is hit it will destroy all of the drone with it.
Main Ship Movement
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.
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.
The Drone Bomber is a child of the Main Ship, so will move with it.
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.
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.
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.
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.
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 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.
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.
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.