/Unity3D – Your First Game – I

Unity3D – Your First Game – I

On this part, you’ll create your first part of your first 2D game using Unity3D game engine. if you’re not familiar with Unity3D, please try to read more about it before starting.

  • Open Unity3D and create New Project.
  • Set Project name and location.
  • Select 2D.
  • Then Create Project.

createproject
Setting up your environment:

  • Right click on assets > create > Folder.
  • Create three folders “Sprites”,  “Scenes” and “Scripts”. Sprites folder to save all sprites and art of the game, Scenes folder to save your game scenes and Scripts folder to save all the scripts of the game.
  • Drag all your sprites in Sprites folder.
  • Save scene by clicking File > Save Scene in Scenes folder.
  • Right click on Scripts folder > Create > C# script, name it “PlayerMovement”. We’ll use it later.

Here is a screenshot that clarifies the structure of the project:
Structure
 
Open your Sprites folder, select all your Textures, make sure that all of them are of type “Sprite”, if not, then convert them to Sprite and click Apply.
Texture
 
Set up your game:

  • Drag the ground into your Scene window, you’ll find that the sprite added directly to your project hierarchy.
  • Add the player and your background the same way.

 
Screenshots of the hierarchy and Game windows
 
hei
 
scene window
 
Every element in the hierarchy has Position, Rotation and Scale properties. You can edit every element as you want it to appear in the scene window.
 
Here is an example of the player position, rotation and scale:
 
props
We’ll go through the other properties of all elements of the game in this part and the other following parts, Don’t worry 🙂 .
 
 
The Directional light element that you see in the hierarchy window. This lights up the scene instead of being dark, you can set the properties of the light like color, intensity and strength from the inspector, try them and you’ll see the difference.
 
Choose the ground from the hierarchy, then click on Add Component > Physics 2D > Box Collider 2D. We add box collider to the element in order to control if we want to trigger this element or not. We added the box collider to the ground to prevent the player from falling.
 
box
Now choose the player > from the inspector click on Add Component > Physics 2D > Rigidbody 2D. This rigidbody component adds physics to the player as Mass and gravity. We added the rigidbody to make the player be able to fall as he has gravity and Mass.
 
rigidbody
Let’s add some mechanics to the player, moving left and right. Open “PlayerMovement” script:
We are going to write few lines of code that enables the player to move left and right a through keyboard keys: left arrow for moving left and right arrow for moving right.
 

using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
	float walkSpeed = 10;
	// Use this for initialization
	void Start () {
	}
	// Update is called once per frame
	void Update () {
	if (Input.GetKey(KeyCode.LeftArrow))
                this.move(Vector3.left);
        	else if(Input.GetKey(KeyCode.RightArrow))
                this.move(Vector3.right);
	}
	void move(Vector3 moveDirection){
		    transform.Translate(moveDirection * Time.deltaTime * walkSpeed);
	}
}

 
We control the movement with keys by writing code in the update function, the previous code contains walk speed of the player, handling keyboard keys and the moving function that receives the movement direction.
 
Now you have a first prototype of the game and two mechanics of the player: moving left and right.
That’s it for this part. See you in the next part of completing your first game with Unity3D.
 
Feel free to ask 🙂
 
 

I have a Bachelor degree of computer science. Also, graduated from ITI (Information Technology Institute), Smart Village Branch, Games Development track year of 2014. Currently working as a game developer in Software and Systems company called Fixed Solutions. Founder at 2024Studios.