2D Galaxy Shooter — New Enemy Type: Aggressive Enemy
Hello all!
Today, I will go over how to make an Aggressive Enemy using Vector3.Distance! This is used when you want to measure the distance between two game objects, so it’s useful for making zombie behavior and such.
You’ll see soon enough!
Creating the Aggressive Enemy
The Aggressive Enemy is basically the normal enemy, but looks damaged and will ram into the enemy if close enough.
- Drag an Enemy prefab in hierarchy.
- Unpack the prefab.
- Rename it Aggressive_Enemy.
- Make a new C# script named AggressiveEnemy.
- Remove the Enemy script, and add the new script.
- Re-reference the audio source, prefabs, and such.
- Add Player damage animations to the Enemy like above so it looks damaged.
- Add these fires to an empty game object, then make this a child of the Aggressive_Enemy. This will make life easier later.
AggressiveEnemy Behavior
Copy everything from the Enemy script to the AggressiveEnemy.
- The Aggressive_Enemy will not shoot lasers, so you can delete the coroutine that shoots.
- Add variables:
[SerializeField] private GameObject _damageAnim;
private Transform _playerPosition; - Reference the empty parent to the _damageAnim and use GetComponent to get the transform from the player. Remember to NULL check.
- Add a new method, then call it in Update().
5. In CalculateMovement(), take away respawn and start EnemyDeath() once they leave the screen. Since they are already damaged, they shouldn’t come back. This should also make them worth more points!
6. Make this Enemy give 50 points.
7. In the EnemyDeath() method, set the transform _damageAnim to false to make the fires go away after enemy death.
This was how to make an Aggressive Enemy that rams into the enemy! Next, I will make an enemy that has a shield, moves slow, and shoots in a V spray!
Thank you for your time!