How To Make A Simple Game With Unity : For Absolute beginners

How To Make A Simple Game With Unity : For Absolute beginners

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Hello, buddies! Here, we are going to make a simple game I published on WebGL recently. That is not so much good. But we will make a game which is better than it!

Play Color Cube 3D here. and Join Us here

Introduction And Download Unity

image.png

What is Unity?

Unity is a cross-platform game engine developed by Unity Technologies. 51% of game is the play store are made with unity. In this game, we are going to use Unity personal edition.

Why Unity

There are many reasons. It is fairly easy to learn and use more than other engines. And you will never get alone with bugs,questions and anything in Unity. They have many official web sites that can help you such as Unity Answers, Unity Forums and many more.

Download Unity

Go there to download Unity or to to unity3d.com .(Download with Unity Hub)You will need 'Visual Studio' too.

Create your project

unity.PNG In unity hub, you can see a 'Create' button.Click on it. Here, we are going to make a 3D game. Name your project as 'Color Cube 3D' or something and select a location. And hit "CREATE"!

Train You Eyes..

Unity1.png

Now you can see some windows as 'Hierarchy', 'Inspector', 'Scene', 'Game', 'Project' and more

Hierarchy

All GameObjects in the scene are in there. If you select a game object in hierarchy, it will also selected in scene. We can make a GameObject by right-clicking in this window. More in the future:D

Inspector

All the components(Scripts and others)added to a GameObject are in here. When a GameObject is selected in Hierarchy or in the Scene, all the components in that GameObject will show here.

Scene

This is the scene view of the game. All the things are done in the Scene view. Move objects, Setting up the scene and more.

Game

This is the full game view. Game view is depending on camera. You can't edit anything in the Game view. When you hit the play button, you can play the game in Game view. You can't save anything while play. If you changed something when the game is going on, its only for that time. When game is stopped, you will lost your changes.

Project

Scripts, Prefabs, Materials, Sprites, Animations, Sounds and all are in Project window. All of these are 'Assets.' You can use folders to make it tidy.

Animation

You have to make all the Animations here.

Console

In console,it will show you all the errors and bugs in scripts and the Editor. Also "Debug.Log" messages are in there.

Don't worry if you didn't understand somethings. All will explained! Stay relax and funny! And take a deep breathe and ready to start!

Set Up Your Scene

Main Camera - Game view is depending on Main Camera.You can move, rotate and anything to the camera to set up the view

Directional Light - This is the main light souse of the game. You can change it too but nothing will change. If you want to see it, select Directional Light and press 'Del'. Did you see it? Now press Ctrl + Z haha.

Making Game Objects

  • Go to Hierarchy and right-click. 3D Object > Cube.You can see a cube on the scene and the game. Rename your cube as "Player".(Normally cube will appeared in front of camera, so it can be seen in-game view. If not, adjust your camera or cube.)

  • Move your gameObject if you want. There are 3 axis in Blue, Red and Green. Blue is Z, Red is 'X, and Green for 'Y'.

  • And, in the inspector window, there is the scale of the cube. You can change it. I will fix it to 5x5x5.

  • To create a color material,go to project window, right-click and Create > Material. And make your color by click the rectangle in white color. (By clicking it,color mixer appear.)

  • Next, drag and drop your color material into the cube

  • Add 'Player' tag to your Player Cube.

image.png

  • After that make another cube and rename it as "Ground". Give him a color if you want. Now, make the 'Ground's scale somewhat big.
  • Now let's add a 'slippery nature for our ground. To do that, right-click on the Project tab, Create > Physics Material. Rename Physics Material as 'slippery.
  • And next, Go to inspector window of 'Player'. And there is a button called 'Add Component'. Click it and type 'Rigidbody' in search bar. And click on Rigidbody. Now Rigidbody is an component of 'Player'.

  • One more thing.to make the game's background as a solid color,go to Main Camera's inspector window and change 'Clear Flags - Skybox' to solid color. And choose your color in 'background' color mixer.

Your game view would be like this,

image.png

Tips

  • To zoom in/out an object, select the object an press 'F'.

-To look around,Click left mouse while pressing 'Alt',and look around.(Like a shooting game.The difference is you have to keep pressing mouse and Alt)

  • Use the WASD key to move while the right-clicking the mouse

  • To maximize the Game view, press Shift + Space.

Adding Player Movement

Let's make the player's movement!

  • Go to Project window, right click, Create > C# Script. Name your script as 'PlayerMovement'.Keep remember - Don't use spaces and special characters. Unless it will give errors.

  • Then double click on script. It will open in Visual Studio and will look like this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

You may be a beginner at coding too. Don't think more about these tags and functions. Remove the 'void Start()' function.

  • First of all,we need to make some public variables for Player's rigid body and to 'forward force and 'side force amounts. So we can change them in inspector easily.
 public Rigidbody Rb;
 public float forwardforce = 2000f;
 public float sideforce = 500;
  • After that,change 'void Update()' function to 'void FixedUpdate()'.And let's add a forward force to our Player like this .
    void FixedUpdate()
    {
        // Adding force to game object
        Rb.AddForce(0, 0, forwardforce * Time.deltaTime); //Since every desktop/laptop has not same number of Frames Per Sec ,we use Time.deltaTime.

    }
  • Now save a script (Ctrl + S) and go to unity.Wait till it updates scripts.(A loading circle will work in right corner of the Editor.That means it is not ready to play)Don't forget to drag and drop your script to Player. After that, in player's inspector you can see some new slots like this,

image.png

  • In 'Rb' slot,drag and drop your Player's Rigidbody component. And hit PLAY button or press Ctrl + P. Now you see what happens. Adjust your speed and see if you want. Now, Let's add a side force by pressing keys.
  if (Input.GetKey("d"))
        {
            Rb.AddForce(sideforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);

        }
        if (Input.GetKey("a"))
        {
            Rb.AddForce(-sideforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("s"))
        {
            Rb.AddForce(0, 0,-1000 );  // 0 to x, 0 to y and -1000 to z. Ordered as x,y,z.
        }
        if (Input.GetKey("w"))
        {
            Rb.AddForce(0, 0, 1000);
        }
  • Adjust player's speed as you wish. But Cube still rotates no? Freeze rotation in 'x' axis, using Rigidbody's Constraints.

image.png

Now, Player's Movement is Okay!

Make Camera To Follow Player

  • This is simple. Create another C# Script and Name it as 'CameraFollow' or any name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public GameObject playercube; // Making a public variable for Player

    private Vector3 offset;
    // Start is called before the first frame update
    void Start()
    {
        offset = transform.position - playercube.transform.position;  
    }

    // Update is called once per frame
    void LateUpdate()
    {
        transform.position = playercube.transform.position + offset; //Make Camera to player's transform position
    }
}
  • Save the script and go to Unity. Wait until it updates.Drag and Drop the script to the Main Camera. Now, you see new slot in Inspector. Drag and drop Player cube to there. Hit PLAY !

Make Like Cubes and Obstacles(Main Level Design)

  • This takes some time. According to our game,we have to put like cubes and Obstacle. Make Like cubes in Player's same color and Obstacle in Black color. Add Rigidbody to all of them.Set the mass as 0 for small ones. -Make a tag named obstacles and add it to an obstacle. They may be in different sizes. It is easy to make a one like cube and a Obstacle first and add those.

  • Make an Empty GameObject(Hierarchy>Create Empty). Rename it as 'Like Cubes'. Make another as 'Obstacles'. Next, drag and drop your Like Cube to 'Like Cubes' gameObject and Obstacle to 'Obstacles' gameObject.Now, they are children of 'Like Cubes' and 'Obstacles'. Now create your first level by duplicating those.

  • If you make one row, you can select them all and duplicate them. So it won't get so much time. Remember to make it somewhat difficult.

Here is mine..,

image.png

Game Over..

Let's make the GameOver part

Making Game Over Panel

  • First, Let's create a GameOver panel. Hierarchy > UI > Panel.Set the Panel Size and Color.In Color Mixer, Set 'A' to 255. You can see the difference. It is useful when making Animation. Now create a Text as GameOver.(Hierarchy > UI > Text). Make 2D view toggle on.

image.png

  • To add Fonts, Project > Import New Asset. Remember to make Canvas Scaler to "Scale with the screen size". image.png

Here is mine one..Making a prefab for the GameOver panel will be better when making new levels. To make a prefab, Drag and drop the GameOver panel to the 'Project' tab.

image.png .

  • Making a prefab for the GameOver panel will be better when making new levels. To make a prefab, Drag and drop GameOver panel to the 'Project' tab. By making a GameObject to a prefab, we can change all the GameObjects once. image.png

Game Over GameOver and Level Completed functions should be in GameManager.Let's make it.

  • Make an Empty GameObject as GameManager. -Make a Script called GameManager.Drag and drop it into GameManager Object. And make it as a prefab(The prefab figure will be different).
  • Go to File > Build Settings. Drag and Drop your scene to 'Scenes in Build'.

image.png

In GameManager,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class GameManager : MonoBehaviour
{
    bool gamehasEnded = true;

    public GameObject GameOverPanel;
    public void GameOver()

    {

        if (gamehasEnded == true)

        {
            gamehasEnded = true;
            GameOverPanel.SetActive(true);
            Restart();



            void Restart()
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            }



        }



    }   }
  • If you deal with scenes, you can't do anything without 'using UnityEngine.SceneManagement;'tag.

  • Save the script and go to Unity. In GameManager's inspector,there is a slot named 'Game Over Panel'.Drag and drop Game Over Panel to there.

  • Make a GameOver script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameOver : MonoBehaviour
{
    private void OnCollisionEnter(Collision collisionInfo)


    {
        if (collisionInfo.collider.tag == "Obstacle") //If collides with a object having OBSTACLE tag..
        {
            FindObjectOfType<GameManager>().GameOver();//Call GameOver function in GameManager
            FindObjectOfType<PlayerMovement>().enabled = false;//disable PlayerMovement script when gameover
        }
    }
}
  • Drag and drop GameOver script into Player.Now you can see, if you hit an obstacle, Panel will show and the Game will Restart

GameOver when Player Falls

  • In our Player Movement script, add

       if (Rb.position.y < 0.1f) //if Player's position lesse than 0.1f,Game over..

        {
            FindObjectOfType<GameManeger>().GameOver();

        }

Now, the Game Over part is finished!

Level Complete

Let's make level Complete part..

-Make a Cuboid(from a cube;)which is set to ground's width.Name it as 'Game End Trigger'. Scale it Like this,

image.png -Now disable the 'Mesh Renderer'.So it will get Invisible.In 'Box Collider',enable 'Is Trigger'. -Make a LevelComplete panel too. -Make a script as 'GameEndTrigger'.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameEndTrigger : MonoBehaviour
{

    public int levelIndex;

    private void OnTriggerEnter(Collider other)

    {

        if (other.tag=="Player")
        {
            FindObjectOfType<GameManager>().LevelComplete();
            FindObjectOfType<PlayerMovement>().enabled = false;
           SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);//Load Next Level in Build Setting.


        }


    }

}

In GameManager,Add

public GameObject LevelCompletedPanel;


    public void LevelComplete()

    {
        LevelCompletedPanel.SetActive(true);
    }
  • Now save and go to Unity. Apply LevelCompletePanel to GameManager's slot. Hit PLAY and play the game. When the Player passes the Trigger, Level Complete UI will appear. Make Prefabs from GameOver Panel, LevelCompletePanel and GameEndTrigger.
  • Next,it is time to make the next levels. Select your first level and press Ctrl + D. So levels will duplicate. Change next levels and make them harder.I will make 3 levels for now. You can make more!

Animations..

Let's make some animations for our panels.

  • Open the Animation tab.(Ctrl + 6)Select the GameObject(Panel)and hit "CREATE".(Make a Folder called 'Animations project tab.) -Hit Record Button first. And go to Panel's color mixer.Set 'A' to 0. image.png -Now go to 40 seconds and set 'A' to 255. Now you have to make a Fade Animation for your GameOver Panel. image.png image.png

-Play the animation and see.We don't need to loop the animation.Disable 'Loop Time'. image.png

-Do the same thing to LevelComplete Panel.One thing should be added.We have to add an event to load 'Next Level'. To do that, go to the end of the Animation and hit 'Add Event'.But we want a script for that;) Make a C# script called "New Level Load".

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NewLevelLoad : MonoBehaviour
{

    public void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //Load the scene which has scene index +1 than the current scene
    }

}
  • Drag and drop the script into the LevelComplete panel.Now, hit 'Add Event'(only if you didn't add it before;). Scroll down and select 'Next Level' function. uni5.png Forgot one thing:D. Add your new levels to 'Scenes In build'.. -Play the Game and see...Well done!

Adding Menus, Buttons and Audio

Start Menu

  • Let's make a Start Menu first. Make a scene called 'Start Menu' (Project > Create > Scene). Make a panel and design it as you want. -Add 2 buttons as 'Start' and 'Quit'.(Hierarchy > UI > Buttons) -Make an empty object as 'Buttons' -Create a script as 'ButtonsInTheMenu'.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ButtonsInMenu : MonoBehaviour
{
  public void StartGame()

  {
        SceneManager.LoadScene(1); //Load first scene
  } 

  public void Quit()
  {
       Application.Quit(); //Quit the application
  }

}
  • Drag and drop the script to 'Buttons' object.
  • In buttons, you can see something like 'On Click()'. Click '+'.
  • Drag and drop 'Buttons' GameObject in to that and select 'Start Game' function to 'Start' button and 'Quit' function to 'Quit' button.
  • Make it as a prefab

image.png

Game End Menu

  • Make a scene as 'Game End Menu' and design it as you want.
  • Make 2 buttons as 'Quit' and 'Restart'.
  • Use same 'Buttons' object and script.(Add prefab)
  • Add 'Quit' button and add same function as 'Start' menu.
  • Make a 'Restart' button.In 'ButtonsInMenu' script,
public void RestartGame()
    {
        SceneManager.LoadScene(sceneBuildIndex: 1); //Load 1st level
    }
  • Add the 'Restart Game' function to that button. Check and see. Nice!

image.png

Add a Music

  • Project > Import New Asset > Select your audio file. Make an Empty GameObject called 'SoundManager'. Drag and drop your audio file to that.
  • Make it as a prefab and add it to the next levels. Well done! Only a few steps to finish our game..

Finishing

  • End of our game. Let's add some designs.(I will only add a one;)

Make two support bars in Level 1.

  • Make 2 GameObjects(Cuboids from cubes). They are the support bars.Fix them on both sides of the ground(so Player won't fall. But don't add it for future levels.You know why.. ;)
    • Make a material. To make it glow, enable 'Emission'. And adjust your color as you wish.

image.png

Build Your Game

  • So, this is the end.Now let's build our game.To do that, File > Build Settings.Add all of your scene in order.

image.png

  • Go to 'Player Settings' and Name your Game and Company.This is not essential for build.You can give a Badge too.

image.png -Next, Select your platform and hit 'Build'. Make a new folder(If you haven't) and select.Wait till it finishes build. That's it!

Conclusion

The game industry is still the newest publishing art form out there, and is constantly innovating and expanding, so it can be tricky to define what exactly a video game is, and what it means to be game developer.

Happy Coding!

 
Share this
Proudly part of