Day 47 — Improvements: boss enemy part 1
Hey and welcome!
We’re onto the last bit that I’m going to implement into our 2D game before moving onto a new project and it’s a big one!
The goal here that I have in mind is to create a boss enemy that spawn at the top of the screen moving down slowly until it stops near the middle of the game scene. Once there it will rotate from the left to the right firing off three lasers from the front along with a multishot attack similar to the player. On top of that the boss will fire off large lasers that will take up the left or right of the screen interchangeably.
First as always let’s start by creating the boss object along with a Collider2D and Rigidbody2D component.
You’ll want to have it a good size, I’ve scaled up one of the common enemies for prototyping purposes. With that done let’s focus on creating a new script for our boss enemy first and work on the movement for it:
private float _verticalSpeed = 1.0f;
private float _rotateSpeed = 16.0f;transform.Translate(new Vector3(0, -1, 0) * _verticalSpeed * Time.deltaTime);if (transform.position.y <= 2)
{
_verticalSpeed = 0;
if (_isRotatingRight == true)
{
transform.Rotate(new Vector3(0, 0, 1) * _rotateSpeed * Time.deltaTime);
if (transform.localEulerAngles.z >= 30.0f && transform.localEulerAngles.z < 35.0f)
{
_isRotatingRight = false;
}
}
if (_isRotatingRight == false)
{
transform.Rotate(new Vector3(0, 0, -1) *_rotateSpeed * Time.deltaTime);
if (transform.localEulerAngles.z < 330.0f && transform.localEulerAngles.z > 325.0f)
{
_isRotatingRight = true;
}
}
}
Reading through this code you’ll see that we set the _verticalSpeed of the boss enemy to 0 when its y position is less than 2. This causes the boss to stop in place just above the middle of the screen. With that done we then add in some rotation movement using transform.Rotate and then we use if statements and a _isRotatingRight bool to constrain the rotation within a local rotation of about 60 degrees back and forth.
Next up let’s add in some code to let the boss survive 12 hits from the player’s laser as well as show a visual cue at 8 and 4 health to show the damage taken.
private float health = 12;
[SerializeField]
private GameObject _leftEngine;
[SerializeField]
private GameObject _rightEngine;private void Damage()
{
_health -= 1;
if (_health <= 8)
{
_leftEngine.SetActive(true);
}
if (_health <= 4)
{
_rightEngine.SetActive(true);
}
}
Make sure to call this Damage() method in the collider code whenever the boss collides with the player or laser. If you remember from before when we had visual damage for the player we used some vfx to show that by parenting it to the player object and then unchecking the active box and that’s what we’re doing here for our boss enemy as well.
With that done it’s simply a case of reducing our health value by 1 every time that the damage method is called. When it drops from 12 to 8 we’ll set one of our engine game objects to active and then the other one as well when the health drops to 4.
With the movement and visual damage implemented we’re ready to look into adding in the attacks for our boss which I’ll go over in the next article.