Learn to code by making a 2D space shooter game.
Objective: Implement Boss AI, a final wave that includes enemy Boss, and add a unique attack.
Before doing that am gonna drag the boss sprite to the hierarchy, add a rigid body, and box collider2D. Set is triggered on the box collider option and set gravity to zero at the rigid body.
In the first step create an empty game rename it, boss, then create another empty game object rename it a gun, drag gun, and boss sprite to this empty Object which is Boss. Create a new script called Boss and attach it to the boss game object.
Here we are going to position our gun object where we need the laser to be fired.
Add a Gun script to the gun game object, this will be responsible for firing a fireball.
Duplicate this gun to make many guns, and rotate them to form a cone of guns so we can fire random bullets in the form of a cone.
Drag Laser sprite in the hierarchy adds a new script rename it as FireBall, and add a behavior to move the fireball down but first create variables of types of float called move speed and of Vector2 called it fireball direction. When the fireball is far from the scene we destroy it.
We need to create a fireBall variable inside a gun script to make the guns shoot the fireball.
Create a public method for shooting a fireball, and instantiate a fireBall inside it.
To make the fireball move toward the direction of guns we specified we need to set the rotation inside the gun, we do this using transform.locaRotation, this is the rotation of the transform relative to the transform rotation of the parent since our gun is inside Boss ship. But first, create a vector3 variable called direction then Inside the update, we set the direction where we want to face plus the rotation relative to the parent game object not transform. rotation. We do this by set direction equal to transform.localRotation multiply by transform.right.
Note: Since it's a direction we need it to be normalized so its length is one.
It's time to rotate the instantiate bullet, we do this by accessing a fireball and setting the rotation to the direction the gun is rotated, we do this by setting the fireball direction to the direction of the gun.
Go to the boss script, create an array of guns, then get all components in children of type gun.
Now we need to create two variables of type float can fire and fire rate and set the rate you want.
Create a method called ActiviateBossFireball, loop through all array of all guns then call shoot method.
Then we check if the time is greater than can fire inside the update.
Select Gun and drat the fireBall to the field in the inspector.
Don't forget to damage the player when the fireball hit the player.
we need to move Boss down the to the center and move left to right when he reaches the center, we do this by using translate to move down and Mathf.cos to move him left and right. When the boss reaches the center we make him stay there at y position.
Here is where the boss starts to fire, when he reaches the center of the screen, we check if the time is greater than can fire, and if the boss ship is at the center of the screen fire the bullet hell(fireball).
Go to make boss wave, I have cut one variable and moved it to my custom class, then to instantiate this prefab first we must access the custom class to get the array of enemy prefabs from the custom class.
Assign the prefabs again to individual waves and the last wave assign the boss.
To make the Boss more challenging, add more unique attacks, add another empty object called beam group drag fireballs inside it, duplicate a fireball couple of times and arrange them into a vertical row then duplicate that group and reposition the second group one to the left and first group to the right big wing of Boss.
I want this laser to have unique behavior, so it moves like a snake, but first let's move them down using translate and destroy them when we hit a player or go beyond the screen.
For snake movement, I have used Mathf. cos to move this laser right to left then I take the Mathf. cos movement of y position of our lasers and assign to the x position so it moves along x and y at the same time then update our position to current position.
Note: while they go down but there is an issue if we do this the laser will collapse even if we have two groups.
To solve this we need to record the position of each laser when there are in a scene and then assign that position with transform.position.x, this is how we solve this issue successfully.
To give the boss health first create two variables of health and current health and initialize the current health to health the value of health I will set as 20.
I want to give this enemy health so that when he reaches a certain percentage he goes crazy. Then I will create a method to decrease this health when a player laser or missile hit us.
Time to fire Wave Laser, we do this when the boss reaches the center and 10 of his current health.to make this laser fire at the time we call this method when check time is greater than can fire.
When the boss appears am always scared due to his long health bar, so tame things fun I gonna add a long health bar but I won't go into detail go check my previous article you will learn how to Implement this. I have created a slider and anchor to the left of the screen
Create a script attached to the canvas, when the game start disables the health bar, set the value max and min value, and call the decrease health bar when the boss gets hit. Also due to the slider component when health is zero, there is the amount of health display so I disable the image fill when health is zero.
Here is where I call this method inside the boss script.
Let's see the final result.
Thank you for reading up here, see you in the next article.