How To Code Unity
Author of Article: Noah R
Intro
Have you ever wondered "How excactly do I make a game" or "Wow, what if I could make that into a game?" Well here is your answer, this article. Just with a few clicks of a button, you will be running Unity after downloading it from this link:
Unity DownloadFirst, make an account with unity. Next, select your operating system. Then, download the Unity Hub. After, log in with your account on the Hub, create a license, and download a Unity Editor. This article teaches with 2022.3.1f1. Keep in mind changes may occur with future Editor versions. Finally, create your first project and name it whatever you'd like. This article will show you how to make a 2D parkour game.
The Player
To create the first object, head over to the hierarchy section on the Unity Editor. Right click with your mouse while hovering over a blank section of the hierarchy. Of the tabs that have popped up click on "2D Object" > "Sprites" > "Capsule." (You do not have to do a capsule, that was just my choice.) From here you will now have a GameObject in your hierarchy named "Capsule." Rename this to "Player" by typing "Player" upon creation, or right clicking on the GameObject then the "Rename" button.
Next, we can (this is optional but recommended) change the color of the Player. In the inspector, with the Player selected, go to the "Color" row under the "Sprite Renderer" section. Change the color to a color of your choice, but probably not blue or green, as those colors will be used later on. Now that we have a capsule named "Player" with the color of your choice, we need to add a few Components to it.
Player Components
To add a component, we need to head over to the inspector. This is another section that only shows when a GameObject is selected. To select a GameObject, click on it through the hierarchy. After selecting the player, in the inspector, click on "Add Component" and search for "Capsule Collider 2D." After searching for this, click on the "Capsule Collider 2D" result. Do these same steps with a component named "Rigidbody 2D."
The "Capsule Collider 2D" will add collision effects to the player, allowing it to collide with other GameObjects that have collision. The "Rigidbody 2D" component will add physics to our player, such as gravity and movement. Then, in the "Constraints" section of the Rigidbody2D component in the inspector, check the Z checkbox in the Freeze Rotation row. We will be using these techniques later on in the creation process.
The Level
To create the level, right click on the hierarchy and go to "2D Object" > "Sprites" > "Square." This will create a white square somewhere around the middle of our screen. We can name this "Ground (1)." Set the color to green, like we might have done with the player, by editing the "Color" of the "Sprite Renderer." Now, Position this at the bottom of the screen by switching to the "Scene" tab, if its not already showing, selecting the move tool, and using the arrows to align it with the bottom of your screen. Finally, use the scale tool to scale it horizantally so it fits the screens' width.
Now that we have the ground positioned so, create three more ground objects by right clicking on the "Ground (1)" GameObject in the hierarchy and clicking duplicate. With these three objects, scale and move them so they fit the other three sides of the screen, but still outside of the cameras' outline. Finally, mass select the four "Ground (1-4)" GameObjects by holding CTRL on your keyboard and clicking on them in the hierarchy, then add the "Box Collider 2D" component to the four objects. *Note: Mass selecting these allows you to add the component to all four at the same time*
Scripting Movement
Now, in the "Project" section of the Unity Editor, right click then go to "Create" > "C# Script." This will create the script we will be using to handle the end goal and movement. The script can be opened by double clicking on it. It is advised you have Visual Studio or vsCode, as these are code editors. Remember to properly indent the script by adding tabs for new sections, as in for the inside of an if statement or a function. The script should contain this (this takes place of where the "void Start()" and "void Update()" are):
public float x;
public GameObject player;
public Rigidbody2D rb;
public bool CanJump;
public void Update()
{
if(Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A))
{
x = 0f;
}
else if(Input.GetKey(KeyCode.D))
{
x = 7f;
}
else if(Input.GetKey(KeyCode.A))
{
x = -7f;
}
else if(!Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
{
x = 0f;
}
player.transform.position += new Vector3(x * Time.deltaTime, 0f, 0f);
if(Input.GetKey(KeyCode.Space) && CanJump)
{
rb.AddForce(Vector3.up * 4, ForceMode2D.Impulse);
CanJump = false;
}
}
public void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "ground")
{
CanJump = true;
}
}
Click and drag this script from the project section to the "Player" on the hierarchy. Now, go to the inspector section for the Player, and define the "rb" and "player" variables by clicking and dragging the Player in the hierarchy to the variable content slots.This script allows the player to move left and right, as well as jump. The input keys are A for left, D for right and Space for jump.
Ground Tags
Did you notice how in the script, there was a "tag" comparision statement. A tag it like a name tag for GameObjects. To be able to jump using the script, but not to double or triple jump, we need to set the tags for the ground objects (Not the ground objects used on the edge of the screen) by clicking on a GameObject in the hierarchy, and in the top right of the inspector clicking "Tags" > "Create new tag." This will bring us to a tag creation section. To create the tag, click on the + button on the array of tags. Then, set the name to "ground." No, there is no period, and no capitals. To assign the tag, select the "Ground (1)" GameObject, and click on the Tag dropdown in the top right of the inspector then the "ground" option.
The reason we need this tag is to detect collision with the ground objects. The easiest way to do so. Next, we will work on the level design, and then the end goal.
Level Design
First, move the Player to where it is touching the ground at the bottom where you'd like it. I'll leave this step up to you. Duplicate the "Ground (1)" GameObject as many times as you want, these will be the platforms, and scale/move them to your liking. I suggest using playtesting to see if the Player is able to make it across any gaps. To playtest, click the play button at the top middle of your screen, and watch the Game section.
Remember, make all platforms reachable, get rid of possible shortcuts and have an end goal in mind. The end goal will be a coin placed somewhere in the level.
Coin Goal
To add the coin for the end goal, create a new GameObject by right clicking in the hierarchy then "2D Object" > "Sprites" > "Circle." Go ahead and rename the new circle to "Coin." Change the color of the coin to your liking, I suggest a shade of yellow, and create a new tag. Name the tag "coin" and apply this to the "Coin" GameObject.
Move the coin to where you would like it to be. Also, add a Circle Collider 2D component to the coin. This will allow detection for when the player gets the coin. Next, we will go into the players' script and add a method for ending the game.
Scripting Coin
Now, go into the player script and add this to the OnCollisionEnter2D() function. We will put this script inside the curly brackets, but remember to indent this properly. Also remember to assign the new variables.
if(col.gameObject.tag == "coin")
{
coin.SetActive(false);
text.SetActive(true);
}
And add these two in the variables section and the other below the OnCollisionEnter2D() function:
public GameObject text;
public GameObject coin;
public void Start()
{
text.SetActive(false);
}
Reward Text
Finally, right click in the hierarchy and go to "UI" > "Text - TextMeshPro" and name it "text." After that, edit the "TextMeshPro" component settings to make a better, more interesting user reward text. Note that this will create a "Canvas" and we will edit that after this.
Also note that the text objects' Transform component is the position component. Setting the X and Y values to 0 can help with centering. And remember to set the "text" variable for the script in the Players' inspector window.
Canvas Edits
Now, click on the Canvas in the hierarchy and edit the Canvas component and Canvas Scaler component to match the image:
Note that the Render Camera row is the Main Camera object from the hierarchy.
Building
After a few minutes of playtesting and editing different things, go over to the File tab of the window then Build Settings.
When you are here, click on the Build button in the bottom right. This will create an executable file in the directory you chose formatted for your operating system, e.g. Mac, Windows, Linux.
Finished Product
Hopefully you were able to successfuly build the game. When finished, go to the directory the game is located in and click on [Your game name].exe and your game will run! The "UnityCrashHandler64.exe" is only there to handle things when the game crashes, just so nothing bad happens. When you have the game running, go ahead an try it out. Does your player move? Jump? What about the parkour, is it reachable? Can you get the coin? Does it display the ending text? If all of these are true... you have successfuly made a video game! Nice work!
All the terms "Unity," "Unity Hub," and "Unity Editor" are copywrited under Unity Technologies
Please send a picture of your work to my email, this can be found on my home page, along with a picture of the end results and what article(s) you used for information to enter the Accomplishments Raffle. View more information on this at my home page.