Create your first game in Unity 3D

Codemaker
Codemaker
May 2 · 12 min read

Unity is a cross-platform game engine developed by Unity Technologies, first announced and released in June 2005 at Apple Inc.’s Worldwide Developers Conference as a Mac OS X-exclusive game engine.

Today, Unity boasts more game development than any other game technology. Unity can be used for both 2D and 3D games. You can do programming in C# and Javascript. It supports for almost 30 platforms including mobile, virtual reality, desktop and console, Unity isn’t just a good place to start, it’s the perfect place to start!

In this tutorial, I will walk you through the Unity environment and show you how to make a simple game in Unity. You do not need any coding experience to follow this tutorial. however, it is recommended. This tutorial is based on the RollaBall tutorial on the Unity website. There are a large number of free tutorials that can be found on the Unity tutorial webpage.

Step 1: Create a New Project

Open Unity3D

Close the “Welcome to Unity” window that pops up by default when you open Unity.

Click File — New Project

Select the location for your project. I like to use one dedicated folder to hold all my Unity projects.

Type the name for your project. In the screenshot, the new project is called “Roll A Ball”

See the screenshot for how this should look before clicking the Create button.

Click “Create.”

Step 2: Understanding the IDE

There are five main windows that you are using most of the time with Unity. They are the Scene, Game, Hierarchy, Project, and Inspector windows. See the five pictures at the top of the step for images of each window.

Scene (A) — this is where the game making happens. It shows what elements you have in your game and where they are relative to each other. There is a block in the upper right corner showing the spatial orientation of the scene.

Game (B)— shows the view that the main camera sees when the game is playing. You can test out your game in this window by clicking the Play button in the top, center of the screen.

Hierarchy (C)— lists all elements you have added to the scene. This is the main camera by default. You can create new elements by clicking Create and selecting the type of object you want. This can also be done by using the GameObject dropdown menu at the top of the screen.

Project (D) — shows the files being used for the game. You can create folders, scripts, etc. by clicking Create under the Project window.

Inspector (E) — this is where you customize aspects of each element that is in the scene. Just select an object in the Hierarchy window or double-click on an object in the Scene window to show its attributes in the Inspector panel.

Step 3: Save the Scene & Set Up the Build

Click File — Save Scene. Save the scene under the folder [Project Name] — Assets. Assets is a pre-made folder into which you will want to store your scenes and scripts. You may want to create a folder called Scenes within Assets because the Assets folder can get messy.

Save the scene as Scene or Main or something like that. Here it is mini3dgame.

Click File — Build Settings.

Add current scene to build.

Select desired platform. There are a lot of options, including computers, game systems, and smart phones, but if you are creating projects for the first time, you will most likely want to select Web Player or PC/Mac/Linux Standalone.

Click Player Settings at the bottom of the Build Settings window. This opens the Player Settings options in the Inspector. Here, you can change the company name, the product (game) name, default icon, etc.

Close out of the Build Settings window. You will come back to this when you are ready to finish your game.

Step 4: Create the Stage

The simplest way to create a stage in Unity is to add cubes.

To do this, go to Game Object — Create Other — Cube, or use the Create menu in the Hierarchy window. Add a cube.

Reset the cube’s transform by right-clicking “Transform” in the Inspector panel. It is good practice to do this whenever you create a new Game Object.

Select the cube in the Hierarchy. Rename it “Wall” by double clicking its name in Hierarchy or using the Inspector panel.

Scale the cube in the X direction to make it long and wall-like.

Right click “Wall” in the Hierarchy panel, and duplicate it three times, so you have four walls. It will look like you only have one wall because they are identical and therefore occupying the same point in space. Drag them into position and/or use the transform options for each cube to make an arrangement that looks like an arena.

To look around the scene view, click the middle mouse button to pan and scroll to zoom in and out. Click and drag while holding the ALT key to rotate the view.

Create an empty Game Object, using the Game Object dropdown (Create Empty) at the top of the screen. Call it “Stage.” Reset its transform.

Select all four “Walls” and drag them under the “Stage” Game Object.

Add a plane Game Object by selecting Create in the Hierarchy panel and use it for the floor. Rename it “Floor,” and drag it under Stage in the Hierarchy.

You need to hit enter after renaming, or else the change may not take effect.

Give the floor a -0.5 transform in the Y-direction to ensure it lines up neatly with the four walls.

Make the floor’s scale in the X, Y, and Z directions 1/10 of the scale you used to size the walls.

Step 5: Create the Player

Go to Game Objects — Create Other — Sphere.

Select the sphere in the Hierarchy, and rename it “Player.” Reset its transform.

Now we need physics. Make the player subject to the laws of physics by clicking Add Component at the bottom of the Inspector panel with the player selected. Add Physics — Rigidbody. Leave all the default settings.

You will notice that each object comes with a variety of “components” added to it that you can see in the Inspector. Each cube, sphere, etc. has a component called a “collider.” This is the physical area of the screen where that object is considered to take up space. If you turn off a collider, than the object becomes like a ghost, able to pass through other objects. (See video for what happens when you turn off the player’s collider component.) You can turn components on and off by checking and unchecking the box next to the component’s name.

Step 6: Making the Player Move Around

/* Developed by Vishnu Sivan */using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {public float speed;
public Text countCubes;
public Text winText;
public Text CollisionPoint;
public List<string> cordinatesList = new List<string>();
public float velocityBall;
public Text velocityBallText;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText(count);
winText.text = "";
}
void Update(){
velocityBall = rb.velocity.magnitude;
velocityBallText.text = "Ball Speed: " + velocityBall.ToString();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText(count);
CollisionPoint.text="Collision point: " + other.transform.position.ToString();
cordinatesList.Add(CollisionPoint.text);
}
}
void SetCountText(int cube_count)
{
countCubes.text = "Cubes collected: " + cube_count.ToString ();
if (count >= 12) {
SetWinText ();
}

}
void SetWinText()
{
winText.text = "Game Over";
}
}

Select the player in the Hierarchy.

Minimize components that you don’t want to see open in the Inspector by clicking the down arrows to the left of the name of each component. This will clear up your workspace a bit.

Click Add Component at the bottom of the Inspector window. Select New Script, name the script something like “PlayerController,” and choose a programming language. I use CSharp. Click Create and Add.

For the sake of keeping files organized, open the Assets folder in the Project window, and create a folder called Scripts. Put your new script in this folder.

To open the script for editing, double click the script’s name in the Inspector, or open it from the Project window. This opens a programming environment called MonoDevelop.

If this is your first time coding, you should know that it can be really nitpicky. Make sure that you are consistent with spelling, cases, and having opening and closing brackets, parentheses, curly brackets, quotations, etc. Also, watch out for errors that result from not having a semicolon at the end of a line of code.

There should already be two sections included in your code by default: void Start () and void Update (). Start runs as soon as the object comes into the game, and update runs continuously while the object is in the game. We will add a third, called FixedUpdate to handle physics-related protocols. It should look like this:

void FixedUpdate () {}

Before we can input commands, we need to declare variables. This is done toward the top of the page, within the curly brackets following Public Class PlayerController: Monobehaviour, but before the void Start() function. For movement, we will use a variable called “speed,” which we can adjust to determine the speed at which our character moves around the arena. Declare the variable type (float) and name (speed) like so:

public float speed;

The semicolon tells the program that this is the end of the line of code. You will get an error if you forget to include a semicolon at the end of every/most line(s) of code, so don’t leave it out!

Under FixedUpdate, declare two more floats, moveHorizontal and moveVertical. These take on values depending on the user’s keyboard commands, and FixedUpdate updates them every frame.

float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);

Case matters.

Still within FixedUpdate, create a new Vector3, a type of variable with three dimensions useful for moving objects around in 3D space. This will take on the value of the user’s input for horizontal and vertical movement, and will be zero in the up/down direction because in this game, the player can only move in two dimensions.

Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);

Finally, input a force on the player to move it around, using rigidbody.AddForce, a protocol built in to the player’s rigidbody component.

rb.AddForce(movement*speed);

Time.deltaTime can be used to make movement smoother. We will adjust the speed variable later, in the Unity editor.

rb.AddForce(movement*speed*Time.deltaTime);

Save the CSharp file, and switch back to Unity.

Go to the Inspector panel for the player, and look at the movement script you have just created. There should be a box for your public variable, speed. You can change the value of public variables using the Inspector.

For now, make speed equal a number between 100–1000, and click the play button at the top, middle of the screen. You should be able to move the ball around using Unity’s default movement keys, either ASWD or the arrow keys.

Click the play button again to exit out of testing mode.

Step 7: Add Lighting

Scene without lighting and with lighting

Directional lights are very useful for creating effects such as sunlight in your scenes. Behaving in many ways like the sun, directional lights can be thought of as distant light sources which exist infinitely far away.

By default, every new Unity scene contains a Directional Light. In Unity 5, this is linked to the procedural sky system defined in the Environment Lighting section of the Lighting Panel (Lighting>Scene>Skybox). You can change this behaviour by deleting the default Directional Light and creating a new light or simply by specifying a different GameObject from the ‘Sun’ parameter (Lighting>Scene>Sun).

Step 8: Adjusting the Camera Angle

We want the camera to be angled down over the arena, so select the Main Camera in the Hierarchy, and adjust its transform until the image in camera preview (the bottom right of the Scene panel, with the camera selected) looks good.

I used (0, 10, -13) for position, and (45, 0, 0) for rotation.

You can also drag the camera around in the scene view to position it, if you wish.

Step 9: Make Items

Create a new Game Object. It can be a sphere, a cube, a capsule, or a cylinder. I used a cube.

Call it “Pickup.”

Tag the Item as “Pickup” by selecting Tags, and creating a new tag called “Pickup,” then going back to Tags for that game object and selecting the new “Pickup” tag that you created. Tag all your items as items. Make sure you match the spelling and capitalization exactly.

Place the item into an empty Game Object called “Pickup.”

Reset their transforms.

Add a rigidbody to the Item.

Create a material by Right Click on project window — Create — Material. and change the color then drag the material into the pickup object.

Drag the pickup object into the prefab folder in the project window. It will create a prefab of the object. Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.

Duplicate the Item a bunch of times and place the copies around the area.

Step 10: Make the Player Collect the Items & Display the Score

Add Text from the Canvas component for displaying the counts. For that Right click in the hierarchy window then Canvas — Text. Type a name like “Counter Text”. Duplicate the Text gameobjects and rename it as Win Text, Collision Point and Ball Velocity.

Open the player movement script from the Inspector panel with the Player game object selected, and modify the script to allow the player to collect, and keep track of, the items it has collected.

Make two declarations: one is a variable that keeps track of your score, and the other is a GUI text that will display your score on the scene view.

public Text countCubes;
public Text winText;
public Text CollisionPoint;
public Text velocityBallText;
private Rigidbody rb;
private int count;
public float velocityBall;

Under the function void Start(), initialize count and SetCountText, a function we will write later.

rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText(count);
winText.text = "";

Write a new function for what happens when the Player collides with the Items. This should be its own section, just like the void Start() and void Update sections.

void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText(count);
CollisionPoint.text="Collision point: " + other.transform.position.ToString();
cordinatesList.Add(CollisionPoint.text);
}
}

Write the SetCountText function, which will update the score on the GUI display.

void SetCountText(int cube_count)
{
countCubes.text = "Cubes collected: " + cube_count.ToString ();
if (count >= 12) {
SetWinText ();
}
}

Save the code and switch back to Unity.

Select all your items, make sure they are tagged as items, and check the button “Is Trigger” in the Box Collider component of the Inspector.

Also check the “Is Kinematic” button under rigidbody. This prevents your items from falling through the floor, essentially by turning off gravity.

Drag the Text gameobjects into the Count Text box on the Inspector with the Player selected.

There you have it! Your own Unity Game :)

I hope you had fun reading and/or following along. In the next story, we will look into how to build more features and interactions into this App. Stay tuned!

If you are interested in further exploring, here are some resources I found helpful along the way:

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store