Development

Eric Philippot
2 min readAug 14, 2022

--

Enemy Sneak Attack

Post MVP Feature Add # 15

Added a non destructive attack to the enemy. The enemy can now shoot an EMP backwards that will stall all the player’s actions for five seconds, leaving them vulnerable to the enemy.

Not all enemy’s have this ability. At this time, about 1 out of 10 enemies will be able to use this feature.

Enemy script showing that every 1 out of 10 enemies will gain either a shield or a EMP bomb.
Enemy Script — Enable Power Up

So this code above just picks based on the number if it is going to be a shield or a EMP bomb the player can use to do a cheap shot from behind on the player. The EnableBackShot() is in Update() so to keep track of the positions and firing times. The _altShotEnabled bool is just there to allow some enemies to use it.

void Update()
{
EnemyMovement();
if (_altShotEnabled)
{
EnableBackShot();
}
}
Both methods that allow the enemy to enable it’s shields or create and EMP attack on the player, leaving them unoperable for 5 seconds.
Enemy Script — Enable Back Shot & Shield

Above we have a check to see if the enemy is past the player, and if so we calculate the distance on the X axis, assigning it to _distanceToEnemy which I will change to player… Next we check to see if the fire rate will allow us to fire and if the calculated distance from the enemy to the player is less than 0.5 units on the “X” axis.

If all conditions are met, we add 6f to the fire rate so we don’t get spammed with EMPs, we instantiate and store a reference to the EMP. Finally we set the speed of the EMP to negative 3 so it goes upward. Why? Because the EMP is also a negative power up that gets spawned in the power up spawn system.

Finally all this starts with the Spawn Manager as shown below.

Spawn manager shown where the Enemy power up scripts are first called from.
Spawn Manager — Enemy Spawning

As you can see towards the bottom, the newEnemy.GetComponent<Enemy>().EnablePowerUp(); starts it all. The player can now destroy the EMP by shooting it. This is achieved in the PowerUp script as displayed below in the OnTriggerEnter2D().

if (other.transform.tag == "EnemyFire" && _powerUpID != 5)
{
Destroy(other.gameObject);
SelfDestruct();
}
else if (other.gameObject.tag == "laser" && _powerUpID == 5)
{
Destroy(other.gameObject);
SelfDestruct();
}

--

--

Eric Philippot

Game and Web Developer with a joined experience of 6 years.