Top
ArticleCity.comArticle Categories How to make a 3D game in Unity

### How to make a 3D game in Unity

###### Photo from Unsplash

Originally Posted On: https://vionixstudio.com/2022/07/25/how-to-make-a-3d-game-in-unity/

In this tutorial, we will make a simple 3D game using Unity game engine from scratch. We will be going step by step so that even a beginner can follow the tutorial. If you are looking to make your own game then I will suggest completing all the tutorials on our learn Unity page.

Summary of the game we are making

The player will click on the screen to make the ball jump. The color of the ground will change between brown and black. Player can score points by hitting the ground when its brown but if the player hits the ground when its black then its game over.

## Video tutorial on How to make a 3D game in Unity

Note: If you have not installed Unity then follow this tutorial on how to install Unity before proceeding.

## Step 1: Starting a new Project

Open Unity hub and click on Create new project.

Select the project template as 3D and give any name of your choice. I am going to call it “Ball Bounce”.

Set the location to which you want to save the project in your drive.

Click on Create project.

## Step 2: Setting up the Scene

The screen you see in front is the Unity editor. It will have multiple windows like Hierarchy, Scene, Game, Inspector and project. You can learn more about them in our getting started with Unity Editor post.

We need a Ball and a ground.

Go to the Hierarchy window and click on the + sign.

Then go to 3D object>Sphere.

Again, click on the + sign and go to 3D Object>Plane.

Select the Sphere in the hierarchy window and go to the inspector window. Set the transform position of the Sphere to (0,2,-4). Now follow the save procedure and set the transform of the plane to (0,0,0).

Now your scene view should look like this

Select the Ball and go to the Inspector window. Click on Add component and select Rigidbody. Rigidbody will make the ball a physics object.

## Step 3: Creating new Materials

Now let’s assign some colors to our objects. Let’s make the Ball red in color and the ground requires two colors brown and black.

All 3D objects have a material which helps the computer understand how to render these objects. Let’s create new materials and assign them to the objects.

Right click on the project window and select create>Material.

This will create a new material in the project window. Change the name to Ball. Create two more materials and call them Ground 1 and Ground 2.

Select the material in the project window and go to the inspector. Find the property named Albedo and change the Color to the required value.

Do this for the other two ground materials.

Drag and drop these materials on to the corresponding gameobjects in the Hierarchy window.

You should be able to see the preview of the materials in your scene view.

Switch to the Game window and select the camera in the Hierarchy. Adjust your camera’s X, Y and Z positions until the view of the Game window is what you want the player to see in the game.

## Step 4: Setting Up the UI

We need UI to display the score and a simple Menu. Let’s add a text object to our scene by clicking on the + sign in the Hierarchy window and UI>text. In the new version of Unity, it will be UI>Legacy>text. Name it as Score. Similarly add one more text object and two Button objects.

Double click on the score text object in the Hierarchy to focus on it in the scene view. Move the score to the right corner top. Make sure the Game window view is as you want it.

Name the other text object as Game over and name the buttons as restart and quit.

Select the Game over Text. In the Inspector window find the text component and change the default text to Game Over.

Select the child text object of the restart button and change the default text to Restart. Do the same for the Quit button.

Your Game window should look like this

Since the menu and the Game Over text needs to appear only when the game is complete, let’s disable them now.

Select the objects in the hierarchy and uncheck the top box in the inspector to disable an object.

## Step 5: Getting the Player Script ready

Note: You need a code editor to edit the codes. If you have not installed Visual studio along with Unity then you can install Visual Studio Code and use it as your code editor.

Select the Ball in the Hierarchy. Go to the inspector window and click on Add Component. Select new script. Type in the name of the script as Player_control.

Click on the three dots on the side of the script and open it for editing.

Copy and paste the code below. Lines after “//” are comments for you to understand the code.

using UnityEngine;
using UnityEngine.UI;

public class Player_control : MonoBehaviour
{
Rigidbody rig; //variable of Type Rigidbody
public Text tex; //Text variable
public static int score; //Integer that represents score
public float force_mag=5; //Floating point to apply force on the ball to jump
// Start is called before the first frame update
void Start()
{
score=0; //Initialize score to zero
rig=GetComponent<Rigidbody>(); //Accessing the Rigidbody component attached to the Ball

}

// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))  //Scanning for Mouse button click
{
rig.AddForce(Vector3.up*force_mag,ForceMode.Impulse); // Applying upward force to ball
}
tex.text="Score:"+score.ToString(); // Assigning the score to the text

}

}

All the Variables set as public will be visible in the inspector window. You can change their values in the inspector window directly. The text variable is used to display the score. Drag and drop your score game object from Hierarchy window to the tex variable in the Player_control script.

If you want to learn how to code in Unity then check out our basics of Unity scripting article.

## Step 6: Creating the Enemy script

Similar to Player script let’s add another script to the ground, which will be our enemy in this case. Let’s name the script “Enemy_control”.

Open the script for editing. Copy and paste the code below.

using UnityEngine;
using UnityEngine.SceneManagement;

public class Enemy_control : MonoBehaviour
{
public Material brown;  //variable of type material
public Material black;    //variable of type material
public GameObject game_over;  //variable of type GameObejct
public GameObject restart;         //variable of type GameObejct
public GameObject quit_button; //variable of type GameObejct
Renderer ren;  //variable of type Renderer
float timer=3;  //variable of type Float
bool material_set;  //variable of type Boolean
// Start is called before the first frame update
void Start()
{
ren=GetComponent<Renderer>(); Accessing the Renderer component attached to the Ball
material_set=false;  //initialize the Boolean value

}

// Update is called once per frame
void Update()
{
timer-=Time.deltaTime;  //Reducing the Timer value based on the time difference between each frame
if(timer<0 && material_set)  //changing materail when time is less than zero
{
ren.material=brown;
timer=3;
material_set=false;
}
if(timer<0 && !material_set)  //reverting back to old material
{
ren.material=black;
timer=3;
material_set=true;
}

}
public void restarting()  //Function for the Restart button
{
}
public void quit()  //function for the quit button
{
Application.Quit();
}
void OnCollisionEnter(Collision col) // Detecting Ball and ground collision to set score or restart game.
{
if(col.gameObject.name=="Ball")
{
if(material_set)
{
Destroy(col.gameObject);
game_over.SetActive(true);
restart.SetActive(true);
quit_button.SetActive(true);
}
if(!material_set)
{
Player_control.score+=10;
}
}
}
}

Save the script and set all the public variable in the Unity editor.

## Step 7: Programming the UI buttons

Now that we have added the script required for button in our Enemy_Control script, we need to link the buttons to them.

Select the restart button in the hierarchy window and go the inspector property. Scroll down and find the OnClick() event. Click on the + Sign. Drag and drop the ground game object. Now you should see the Enemy_control script in the drop down. Select it and then select the restarting function from the list.

Do the same thing for the quit button.

## Step 8: Testing the Game in Unity Editor

Now that everything is set. Go to File>save and save the scene.

Click on the play button to start the game. Now the ball should bounce when you click on the screen and should fall down due to gravity.

You can keep the ball up by clicking on the screen and get points by allowing the ball to hit the ground when its brown in color. If the ball hits the ground when it’s black in color then the game over menu will appear.

The quit button will not work in the editor but will work in the game build.

## Step 9: Building the Executable File

It’s time to build a standalone executable that you can share with your friends.

Go to File>Build Settings. Select the platform as windows.

Once your platform is set, you will see a build button. Click on the build button and save your game’s executable.

Now you should be able to run your game and test it.

That’s it you have built your first 3D game in Unity. If you have any questions, leave them in the comment box below.