2D Game Dev Journey — Day 2: Prototyping an Enemy
Welcome to 2D Game Dev Journey — Day 2: Prototyping an Enemy. For today, I will be explaining how I created an enemy prototype for the 2D game, which currently I have named “Space Defender”. Definitely feel like that might be taken though, so let me know in the comments if it is. Today I’ll touch on the topics of Physics, Collision Detection, and Script Communication in Unity.
Now one of the beautiful things about Unity is that it comes with a built-in physics engine. With their physics engine comes colliders, an implementation for gravity, physics articulations, and more. Now for the 2D game I plan to make, I don’t plan to use the gravity system they have, but I will be making use of the colliders, so let’s start setting things up for that and I’ll show you a bit of how gravity works here.
For today’s starting point, it is pretty much where we left off yesterday. The only thing I added was another cube to the scene, named it “Enemy”, and gave the “Enemy” GameObject a slightly de-saturated red material. From here, let’s add a RigidBody component to our “Enemy” GameObject.
The RigidBody component has a decent amount of properties on it, and I recommend you play with them and learn about them in-depth. For now, keep the Use Gravity option checked. If you are following alongside me, before playing around with gravity, I suggest creating a script called “Enemy” and attaching it to your “Enemy” GameObject. Within that code, have the enemy loop back to the top of the screen once they have gone off the bottom screen. You should have a result like the below:
As you can see above, the red cube is falling due to gravity, which can be seen by the constantly increasing velocity the GameObject is experiencing due to the acceleration due to gravity. We even get to see collisions starting to get taken into account, which is demonstrated by the “Enemy” GameObject stopping when it comes into contact with our “Player” GameObject. From here on out, I’ll have the Use Gravity property off, so implement some code to move the “Enemy” GameObject down each frame. You should get a result similar to what I have here:
Once you are happy with what you got, let’s move on to the next section.
As I stated in the previous section, we already see collision at work given our current setup. Now what we want to do is to give ourselves the ability to destroy our “Enemy” GameObject with our projectile prefab. In between the previous section and this one, I added a RigidBody component to my projectile GameObject. Here is the result when we run things without any additional code:
Definitely some pretty weird results. Let’s start adding some code to get behaviors we expect to happen. For this section, let’s get the enemy recognizing collisions and printing out a statement in the Console window showing what it collided with. In my implementation, I completed this by utilizing the OnTriggerEnter() method, as well as changing the IsTrigger property on all of my colliders to True. The result is as shown below:
Nice! At this point, we have the “Enemy” GameObject recognizing not only that another GameObject is colliding with it, but also the tag/name of that GameObject it is colliding with. In the next section, we will go over how we can destroy the “Enemy” GameObject if it comes into contact with the player, as well as destroying the projectile and Enemy GameObject when both come into contact with each other.
Through implementing OnTriggerEnter(), we can see that a GameObject implementing this method can grab the information associated with whatever object they collided with. Using this knowledge, we can take this a step further to do some script communication between GameObjects. To start, let’s add some code to our “Enemy” script to get destroyed when we realize it is being hit with our projectile or player. If implemented correctly, you should get a result similar to what I have below, where the enemy should just disappear.
At this point we have the enemy being destroyed when they come into contact with the player and projectile. Not too shabby. The last thing we need to do now is decrease the player’s health when they are hit by the enemy, as well as destroying the projectile GameObject that came in contact with the enemy. Now we could do this by utilizing the OnTriggerEnter() function again, but instead, this would be a great time to introduce what is, in my opinion, one of the most useful built-in methods Unity provides — GetComponent(). Honestly speaking, when I first discovered the GetComponent() method, the very first thing that came to my mind was “Game Changer”. This method allows you to grab any component on the GameObject in question, which includes the component of a GameObject we just collided with. With this component now grabbed and referenced, we can manipulate any public variables as well as call any public functions within that component. Let’s use this to our advantage. If you are following along, in the script controlling your projectile, add a public method such as EnemyHit(), and within it, destroy the projectile GameObject. In your player script, create a global int type variable named “lives” and set it to whatever you like (I set mine to three). After that, create a public method named something along the lines of HitByEnemy(), which will decrease the player’s “lives” property by one each time it is hit by the enemy. Once the “lives” property reaches zero, destroy the Player GameObject. Call both of these public functions at the appropriate time from your “Enemy” script. If done correctly, you should have results similar to what I have shown here:
Dope! Now we have collisions going in our game, as well as a player lives system. With that, I’ll end today’s progress update.
Thanks for checking in and reading today’s article. Tomorrow, I will be creating a spawn manager to spawn these enemies over time, so look out for that if interested. As always, comments and critiques are welcome. If I have made any mistakes, please let me know so I can correct them for future readers. If you would like a more in-depth tutorial about anything I went over today, let me know and I will take the time to write something up.