Full ramming speed!

In the last article, I implemented enemy shields. Today, I’m going to be continuing the second, and final, phase of the GameDevHQ 2D Game Development Certification track. In this article, I’ll be adding an aggressive enemy type! The requirement reads as follows:

Tasks:

  • Create the functionality to support enemy aggression
  • If an enemy is close to a player, the enemy will try and “ram” it.

Initially, I thought todays task was going to be simple. Add a second collider to my enemy, then on trigger calculate a vector that would move my enemy towards the player and increase the enemies speed! I had to change tactics when I found out that Unity treats two colliders on the same object as a single compound collider and that distinguishing between them was a fair bit of work and a lot of the solutions didn’t look scalable. After searching around a little bit for an answer I found a solution using the Unity physics library to check for collisions within an area.

To get started I created a new prefab for the new enemy type, copied the collider, sprite render, rigid body and enemy with explosion scripts to the prefab, and changed the sprite for the new enemy type.

After this I created a new wave for testing that only contained the new enemy type, and a new wave config that only contained that one new wave. Then I ran the game to make sure the new enemy behaved as expected without the new ramming functionality.

Once I confirmed this I added a new C# script to the new enemy prefab called PlayerSeeker that would contain the code for seeking and ramming the player. In this script I added some fields for the detection range, the ramming speed, and a reference to the enemy that the script is attached to. I then had to add some code to the enemy class to allow me to access and modify the movement speed of the enemy.

Then, in the awake method, I cached the enemy reference, a reference to the enemy’s renderer, for later changing the enemies color, and the initial movement speed of the enemy, for restoring the original speed if the enemy gets out for range of the player after going into ramming mode.

With initial setup out of the way I started in on the meat of the behavior. In the Update method, I made the magical call to Physics2D.OverlapCircleAll, passed in the enemies position as the center of the search radius and passed in the range to search out from that point. This call returns an array of all of the colliders within range of the supplied center point.

Once I have the array, I check to see if the player is within it. If the player is within distance, I set a flag that activates the ramming state, point the enemy towards the player by calculating the angle between the enemy and the players positions, and increase the enemies movement speed.

Lastly, If the player is not in range of the physics check I restore the default speed and rotation of the enemy.

After some play testing, I have aggressive enemies!

Next Time!

In this article, I implemented an aggressive enemy type. Next time, I’ll be adding a smart enemy type! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

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
Christopher West

Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.