Day 42 — Improvments: new enemy

Hey and welcome!

It’s time to add in a new enemy as well as add in some extra behaviour to our original enemy.

The goal here is to add in an enemy that can only fire lasers from behind and only when it’s behind the player. Then we’ll add in some ramming behaviour for both the new enemy and our regular enemy.

First let’s start off with the new enemy, this time we’re going to update our Enemy script since we’ll have the same movement for both.

private bool _isBackwardEnemy = false;
[SerializeField]
private GameObject _backwardLaser;
public void BackwardEnemy()
{
_isBackwardEnemy = true;
}
private void FireBackwardLaser()
{
if (_enemyCanFire == true)
{
if (transform.position.y < _player.transform.position.y && Time.time > _nextFire)
{
_nextFire = Time.time + _fireRate;
GameObject backwardEnemy1 = Instantiate(_backwardLaser, transform.position + new Vector3(0, 0.5f, 0), Quaternion.Euler(0, 0, 30));
GameObject backwardEnemy2 = Instantiate(_backwardLaser, transform.position + new Vector3(0, 0.5f, 0), Quaternion.Euler(0, 0, 0));
GameObject backwardEnemy3 = Instantiate(_backwardLaser, transform.position + new Vector3(0, 0.5f, 0), Quaternion.Euler(0, 0, 330));
}
}
else
{
return;
}
}

In order to tell if it’s our new enemy I’ve created a bool here called _isBackwardEnemy since our enemy will be firing lasers backwards. In order to determine the value of this we have a public function called BackwardEnemy() that sets the value to true when it’s run. The best time to call this function is when we’re spawning in the enemy in our Spawner script so we’ll take a look at that later.

When it has been determined that it’s the backward enemy then we make use of a new method for firing lasers which is our FireBackwardLaser() you should be able to recognise most of this code but the gist of it is that when the positional y value for our enemy is less than that of our player it will fire off 3 lasers at once, with each of them at a slightly different angle using the local euler angles again.

Now let’s update our wave switch statement in the Spawner script to accommodate for this new enemy.

private int _backwardEnemyCount;
[SerializeField]
private GameObject _backwardEnemy;
if (_backwardEnemyCount < 2)
{
Vector3 backwardPosition = new Vector3(Random.Range(-10.7f, 10.7f), 6.2f, 0);
GameObject backwardEnemy = Instantiate(_backwardEnemy, backwardPosition, Quaternion.identity);
Enemy _backwardEnemyScript = backwardEnemy.GetComponent<Enemy>();
backwardEnemy.transform.parent = _enemyContainer.transform;
_backwardEnemyScript.BackwardEnemy();
_backwardEnemyCount += 1;
}

Pretty similar to our other two enemies but this time we’re creating a local reference to the enemy script so that we can call our BackwardEnemy() enemy method. Don’t forget to update the enemies to destroy value as well to account for the extra two enemies.

With that we have an extra bit of challenge in the game, something that punishes the player for missing an enemy and letting them get behind!