Boss Movement and Health

Christian Carter
3 min readSep 21, 2021

--

This tutorial covers adding boss movement and its health. The boss movement will move part way down the screen and stop and then it will start rotating. The first step is to make sure the boss will start off screen. In the Start function set the position so it will be above the screen.

transform.position = new Vector3(0, 10f, 0);

Next add two global variables at the top. One to track the speed of movement and the other to track the rotation speed.

private float _speed = 2.0f;
private float _rotateSpeed = 20.0f;

Now create a MoveAndRotate function. This will handle both moving down the screen and rotating once it has reached its final position. The movement is very basic. Use an if statement that will stop moving once the y position is where you want it. Then in an else statement start rotating it just like the Asteroid rotates using transform.Rotate.

void MoveAndRotate()
{
if (transform.position.y > 4f)
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
}
else
{
transform.Rotate(Vector3.forward * _rotateSpeed *
Time.deltaTime);
}
}

Make sure you call your MoveAndRotate function inside the Update function.

void Update()
{
MoveAndRotate();
}

That is all you need for the movement and rotation. Now it is time to focus on the health of the boss. To start add three global variables. Add one to track how many hit points it has one to track the player and one for the explosion animation.

private int _health = 10;[SerializeField] private GameObject _explosionPrefab;
private Player _player;

Make sure you assign the Explosion prefab in the Unity editor.

Also, make sure you assign the player in the Start function.

_player = GameObject.FindWithTag("Player").GetComponent<Player>();
if (_player == null)
{
Debug.LogError("Player is NULL.");
}

Next, create a TakeDamage function. First make sure you decrease the health in this function. Then, check to see if the health has reached 0. If it has add points to the score for the player. Then create the explosion. Make sure you match the size of the explosion to the size of the boss. Then destroy the boss on a short delay to give time for the explosion to start.

void TakeDamage()
{
_health--;
if (_health == 0)
{
if (_player != null)
{
_player.AddScore(100);
}
GameObject explosionObject = Instantiate(_explosionPrefab,
transform.position, Quaternion.identity);
Explosion explosion = explosionObject.GetComponent<Explosion>();
explosion.DoubleSize();
Destroy(this.gameObject, 0.25f);
}
}

Next, add the OnTriggerEnter2D function. First check if the boss has collided with the player. If so damage both the player and the boss. Next check if it has collided with a laser. If so then destroy the laser object and have the boss take damage.

private void OnTriggerEnter2D(Collider2D other)
{
if (other.transform.tag == "Player")
{
Player player = other.transform.GetComponent<Player>();
if (player != null)
{
player.Damage();
}
TakeDamage();
}
if (other.transform.tag == "Laser")
{
Laser laser = other.transform.GetComponent<Laser>();
if (laser.GetIsEnemyLaser() == false)
{
Destroy(other.gameObject);
TakeDamage();
}
}
}

That is all you need for boss movement and health. The next tutorial will cover boss attacks.

-Chris

--

--

No responses yet