2D Galaxy Shooter P2: Added an Aggressive Enemy type!

For today’s challenge, I had to implement an aggressive enemy type!

The challenge is to add a new, aggressive enemy type that can ram the player when in front.

For this, I had to restructure the collision detection for each game object. In the Layer Collision Matrix, each type of game object has its layer mask and setup.

Using this system could be a bit tricky at first, but once you get the hang of it, it’s as simple as tieing shoes!

I went toward this route because I want the new type of enemy to detect the player in front of it with a new trigger collider. By adding a new collider in a child object, Unity will still consider it part of the default layer mask, hence, the reason why the player and enemy would take damage.

By changing the layer mask of this new game object with the new trigger collider, I can now control the type of game object’s collision detection through layers!

In the above image, the box collider in front of the enemy will detect the player when the player triggers it.

And when it detects the player, the enemy movement type will change to aggressive mode and double the speed to ram the player.

if(_enemyMovementType == EnemyMovemenType.Aggressive)
_isAggressive = true;
transform.Translate(Vector3.down * (_speed * 2) * Time.deltaTime);
_isAggressive = false;

Once the player exits from the trigger collision, the enemy will change its movement type back to default.

I also set the enemy to fire three projectiles when in default movement. When the player is detected, it won’t fire.

My biggest take on this challenge was solidifying my experience using the layer system! It’s so valuable and will give you less of a headache down the road. It was difficult at first, but after messing with it for a while, it became easier.

That is all for today! Thank you for your time!

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store