5 min read

Creating a Weapon

You will remember that back in Part 1 we made our simple MegaMan clone. Let’s take this project further.

So, first off let’s create a weapon. Now, we’re not going into minute details like creating an actual weapon for our hero, but let’s create a bullet, or else it’s going to be hard to shoot enemies! Create a sphere called “Bullet,” and change all of its scale values to 0.2, attaching a new material to it that will be yellow. Make sure afterwards to make the Bullet a Prefab by dragging it into the Project Assets folder. Lastly, remove the sphere collider, and add a circle collider 2D to it.

Now that we have our bullet, let’s create a new script called “Weapon”, and attach it to our Player Object. We’ll also create another new script called “Bullet” and attach it to our Bullet Prefab. In fact we didn’t do it in the last post, but let’s actually make the Player object a prefab. Now first, open the PlayerMovement script and make a quick edit.

Ok, so we have created a new enum state called Direction, and an associated property called playerDirection, that is going to keep track of what way our player is currently facing. We also created a property, because nothing else but our PlayerMovement script should change our players direction. Also this stops it from appearing in the inspector, which if it was there, could eventually start cluttering things if our designers are not really supposed to be touching that. Lastly, in our MovePlayer method called every update, we add a simple if statement to keep track of what way our player moved last. Note that it is not affected at 0, this is because we want to know the last direction moving, so if our player is at a standstill, we still want to shoot the previous way clicked.

Alright, let’s open our Bullet.cs script and quickly make some edits to it!

So we now have our bullet that will move in a direction based on its own direction state. All we need is a part to manage all of these interactions. This will be our weapon script, so let’s open that now!

Ok, we have what is essentially a manager of these two together. This one will wait for a user’s input, create the bullet, and then set its direction depending on the players current direction. We use the Fire1 button so that this can be changed later in the Input manager and work on other controllers easily. Now, we do want to point out something with our connection between the playerMovement class and the bulletDirection class. First, we have a very tight coupling on these classes, which isn’t great, but for the continuation of this post, we’re going to skip it. But if you to wish know more about this we suggest researching delegates and events, as well as decoupling in Unity. For now though, this will do.

Creating an Enemy

Next let’s create an enemy for this bullet to interact with. So let’s create a cube, make him red with a material, and then give him the tag “Enemy” as well as the name “Enemy”. Take off the box collider, and attach a box collider 2D, as well as a rigidbody2D. Lastly, make this enemy a prefab. It should look like the following in the Inspector.

Now to make sure our player and bullet don’t bump each other anymore, let’s quickly take that out of the physicsManager. First, create three layers, “Bullet”, “Player”, and “Enemy”. Each of these three game objects should be put on their respective layers. Now in the PhysicsManager under Edit _> Project Settings -> Physics 2D, make sure that the player and bullet classes are NOT checked, so they no longer respond to each other.

Okay, now let’s create an “Enemy” script and attach it to the Enemy game object.

In here, we have a very simple script that just contains a health int, and a method to adjust the health of our enemy. Realistically our player class should have a very similar set up, but for the sake of scope, we can just do this for our enemy. Also, when our enemy class takes enough damage, we destroy that game object. Now we’re going to have to change our Bullet script as well to know what to do with this class.

We’ve added a couple of things. First, we now have a damage int at the top of our class that is used to measure the damage this bullet will do to our enemy. We could for example, hold down the shoot button, which increases the damage of our bullet. For this, we’ll just keep it at a base amount. Next, we add the OnCollisionEnter2D method, which is going to handle what to do if our bullet interacts with an enemy. If the collided with object is an enemy, our bullet will call the Damage method in the enemy class, and then destroy itself afterwards. In honesty, we could actually put that destroy outside the if statement so that no matter what the bullet hit it would destroy itself.

So now if we try our game we have an enemy in the game world who after two hits will actually die. Yes I know he’s not really in any danger right now, but this is a great start for finding hittable targets! If this project continued, the next thing added should be a simple enemy movement script, some weapons perhaps for our enemies, and then some simple level design!

For more Unity game development tutorials visit our dedicated Unity page here.

About the Authors

Denny is a Mobile Application Developer at Canadian Tire Development Operations. While working, Denny regularly uses Unity to create in-store experiences, but also works on other technologies like Famous, Phaser.IO, LibGDX, and CreateJS when creating game-like apps. He also enjoys making non-game mobile apps, but who cares about that, am I right?

Travis is a Software Engineer, living in the bitter region of Winnipeg, Canada. His work and hobbies include Game Development with Unity or Phaser.IO, as well as Mobile App Development. He can enjoy a good video game or two, but only if he knows he’ll win!

LEAVE A REPLY

Please enter your comment!
Please enter your name here