6 min read

In this post, we’re going to be making a simple mega man clone. Now that sounds like it’s going to be a huge undertaking, and in reality making an entire mega man clone would be huge, but what we are going to focus on is building a simple shoot functionality and enemies that can be destroyed by a bullet.

Now I’m going to skip a lot of the basics on creating things like squares and such to keep the pace of this higher, so if you need information on some of these types of things, we recommend looking at some of the other more beginner oriented articles on this site. My screenshots for this lesson will be using the Unity 5.0 beta version, but the directions should be applicable to Unity 4.6+. So let’s begin!

Getting Started

First create your new game. Make it a 2D project, and then make 2 3D cube objects for this. Name one cube “Ground”, and the other Cube “Player”. First select the ground object and switch its transform attributes to the following.

While still on the Ground object, add a material to it called “Grey”, and give it the same color, and remove the BoxCollider component in the inspector, and add a Box Collider 2D component

Once you have your cube, resize him so that he’s thinner, as mega man has never been known to be an obese robot. We’re also going to add a Rigidbody 2D, a Box Collider 2D, and a material called “Blue” that is blue for use later. Also for ease, change the tag on the player to the tag “Player”. Great, here is how your inspector should look for the “Player” object.

 border: 1px solid black; display: block; margin-left: auto; margin-right: auto;

Lastly, for simplicity sake, just create a directional light object in the scene, and then drag it out of view. This will just allow us to at least see the seen. With our basic objects in place, try clicking play on the scene and watch what happens! Your player should just drop on to the ground object we created. Perfect, we have gravity and some world bounds, so let’s create movement controls!

Now, Unity comes with a bunch of pre-made controls that we can use, but there is no better way to learn scripting than scripting ourselves! So, first, let’s get some lateral movement going. Create a new C# script called PlayerMovement and attach it to the Player object.

First we’re going to make some simple movement controls using the Input class and use what axis is being pressed to see what direction our character is moving in. This allows us to use only one segment of code for all of our characters movement, making editing later much easier to do. Here is what your code will look like:

 Unity 5, C#, Megaman

We move our MovePlayer into its own method for only the reason of keeping our code cleaner, and allowing it to be easy later to know what is being done by sections of code. Filling up our Update method is only going to cause headaches later. Next, we’re going to implement some simple jump functionality. But before that, looking at this code we already have, we think we should actually start something that, if not learned early on, is a tough habit to build later. What we’re going to learn is references.

These references will hold a link to different components, whether they are attached to the same Game Object or attached to a completely different one. You see, every time you reference any component, it’s going to take time to have to actually search for that component, and then perform the code you have stated to do with it. Doing this will cause small loss of performance each time you do it. While this may not seem like a big deal, this small problem tends to build up over a ton of classes and objects later on, so it’s best to just start with references right when we start. So let’s build some references, as well as add some variables and methods for our jump function.

 Unity 5, C#, Megaman

As you can see in the linked code, we have our rigidbody and our transform now put into 2 private variables that we can use later on that we wont have to travel through our game object to find. Now you may think, “hey I get all these from the gameobject.transform and the gameobject.rigidbody calls, do I need these references?” Well the truth is, every time you right those, they are actually calling the 2 GetComponent calls we do in the Start method every time. So by saving a reference, we make sure that Unity doesn’t have to make these calls over and over.

So lets quickly finish up our jump functionality and test our game! add the following code to get our like character jumping and running around the screen.

Unity 5, C#, Megaman

Alright, you should be able to test this out for yourself, but let’s quickly go over what was used. We created an isOnGround boolean that only let’s us jump if we are on the ground, and a jumpPower float that lets us change our players’ jumpPower for testing and things like power-ups. Next in the Jump method, we watch for when the player clicks space and the player is on the ground, and add force to the object’s rigidbody moving them up in y by our jumpPower. Lastly, we use simple collision detection to see if our player has collided with anything. If they have, then we assume they are on the ground, and allow them to jump again.

Now there are some obvious errors this will cause us later, but for simplicity we have decided to use it for this article, and that is that if the player touches anything else, and this includes a platform above him, he will be allowed to jump again. Now there are a number of solutions for this problem that we may fix in the next post, but for now, this will allow us to at least move and jump our character correctly.

So that wraps up this post. In part 2, we will get our character a weapon and some enemies and start being able to destroy some evil robots. See you then!

For more Unity tutorials make sure you check out our Unity page. Click here and start exploring.

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!

2 COMMENTS

  1. Using unity 2018.2.1f1, at the end of this page, I have compiler error: “CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.”

LEAVE A REPLY

Please enter your comment!
Please enter your name here