Developing 3D Games with Unity 5

Tanay Pant
Share

If there’s one thing cooler than playing games, it’s building games.

Whether you’re building 2D games like Angry Birds, or 3D games like Counter Strike, you can earn money by publishing them at places like Play Store, App Store or on the Web.

How to Get Started Building Games

There are many tools for creating games, and in this article, we’ll look at the very popular Unity 5, a gaming platform that facilitates 2D and 3D game development for a variety of platforms such as OS X, Linux, Windows, Unity Webplayer, Android, iOS, BlackBerry 10, Windows Phone, Tizen, WebGL, PlayStation, Wii U, Nintendo, Xbox, Android TV, Samsung Smart TV, Oculus Rift, HTC Vive and Gear VR.

In this article, we’ll look at how to build a 3D spaceship game full of obstacles, enemies, maneuvering and shooting. After we finish building our game, it will look like this:

Spaceship Game Final

We’ll walk through the installation of Unity3D Personal Edition, develop a 3D game employing 3D modeling, Unity’s resourceful asset store, scripting (JavaScript) and Unity’s powerful physics engine.

There are no pre-requisites for following this tutorial, although a basic knowledge of JavaScript would to be helpful.

Installation

Installing Unity is as easy as downloading the installer for your respective platform from here.

The download manager is a small file that prompts you to select which components you can download. These downloads are typically large in size (in gigabytes), so you might want to sit back or read through this tutorial before your installation process is complete.

After installation, open Unity 5 and select New Project under the Create a Project option. Enter the name of the project and select the 3D option to import the asset packages for 3D games, and finally click on Create Project.

Create Project

Understanding 3D Models and the Camera

If you’ve followed all the steps in the Installation section successfully, you’ll get the Unity 5 interface for creating games. Since we’re building a 3D game, we’ll need to have 3D objects in our gameplay. You can insert a 3D model into the game by selecting GameObject > 3D Object. I inserted a cube into the scene. Your screen should now look like this:

3D Models

Upon clicking on the cube, you’ll see a number of options related to the 3D object you’ve selected – such as Transform, Mesh, Box Collider and Mesh Renderer.

These 3D models interact with each other and the gameplay using what’s called a Physics Engine. You can apply physics rules to any 3D object by clicking on it and selecting Components > Physics > Rigidbody.

This will cause Unity’s gaming engine to treat the object as a real-word Rigid Body and apply various real-world interactions to it, such as collisions and gravity.

Unity uses cameras to render the objects in the game space. When you enter the Game mode to have a look at your game field, you’ll be able to view it from the camera’s viewpoint – which is also what players of the game will see.

The following image shows the field of view of the camera and the camera preview that can be opened by clicking on the camera object:

Camera

It’s worth playing around a bit with 3D models and cameras, as the more familiar you are with them, the better you’ll understand the steps that follow.

Using Unity Asset Store

Since we’ve just started, it’s difficult for beginners to develop very complicated players as well as scenery for the game. Unity’s asset store comes to the rescue to help us solve this problem.

Unity’s asset store is a collection of various models, materials, sounds, animations and more. The assets can be of many types and many models may have animations and sounds attached with them for making your work easier.

Just like any other store, there are free as well as paid assets available for building a game. Since we’re building a spaceship game, let’s get a cool looking spaceship for our game.

You can open the Asset Store by clicking on the Asset Store tab beside the Game tab. I chose a spaceship set for the game and my screen looked like this:

Asset Store

You can also download this asset set by clicking here. Now click on Import to import this asset set to your project. You can bring the spaceship to the gameplay by clicking on omega_fighter_01 > model and dragging omega_fighter to the Hierarchy pane on the left side of the screen. Your model should now be in the game area.

You should now reposition the spaceship at origin (0, 0, 0) by clicking on the spaceship and entering the respective values in the Transform menu on the right pane. You will also need to reposition the camera using the same technique.

For me (0, 0.8, -1.5) with a rotation of (25, 0, 0) worked well. Enter the game mode by clicking on the Game tab and add the Rigidbody character to spaceship. Don’t forget to remove the tick from the Use Gravity option under the Rigidbody option in the right pane (you wouldn’t want your spaceship to fall off the sky, would you?). Your screen should now look something like this:

Game View

Constructing the Game Setting

Let’s construct a simple game setting by setting up some obstacles. Place a ground with the help of Components > 3D Objects > Plane options and add some pillars.

You can develop a maze where your spaceship has to avoid all the obstacles and have to reach the ending wall and has to touch it to avoid being hit. Add rigid body component to all of them. Here’s how my game field looks like from the top view:

Top View

Adding Behavior to Objects

Your game now needs logic to run. You’ll have to program how the spaceship moves, what happens when the spaceship collides and so on.

CameraController

You’ll have to program your Main Camera to move along with your player, otherwise you won’t be able to keep track of your spaceship once it moves out of the camera’s field of view.

From the menu bar, select Assets > Create > Javascript to create a JavaScript file. Now click on Main Camera and select the Add Component > Scripts > CameraController button from the bottom of the right pane.

#pragma strict

// variable offset of the type Vector3 
var offset : Vector3;

// variable player of the type GameObject
var player : GameObject;

function Start () {
    offset = transform.position - player.transform.position;
}

function LateUpdate () {
    transform.position = player.transform.position + offset;
}

The above code will calculate the difference between the positions of the camera object and the GameObject and will update the camera’s position (using the LateUpdate() function) according to the player’s position.

Now, add a script to the Player object.

#pragma strict
import UnityEngine.UI;

var loseText : Text;
var winText : Text;
var rb : Rigidbody;
var speed : float;
var tilt : float;

// initialising variables
function Start () {
    rb = GetComponent.<Rigidbody> ();
    loseText.text = "";
    winText.text = "";
}

function Update () {

  function FixedUpdate () {

    // take input from keyboard
    var moveHorizontal : float = Input.GetAxis ("Horizontal");
    var moveVertical : float = Input.GetAxis ("Vertical");

    // map the movement with Vector3
    var movement : Vector3 = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    
    // add force to the object
    rb.AddForce (movement * speed);

    // add rotation to the object
    rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
}

// works when two objects collide with each other
function OnTriggerEnter(other : Collider) {
    if (other.tag == "Obstacles") {
        Destroy(gameObject);
        Destroy(other.gameObject);
        loseText.text = "You Lose!"; 
    }
    if (other.tag == "Win") {
        Destroy(gameObject);
        Destroy(other.gameObject);
        winText.text = "You Win!";
    }
}

Here we have imported UnityEngine.UI to import the datatype Text, which is present in the UI toolkit of Unity. The above code maps the movements of the spaceship (player object) with the arrow keys (using the Input.GetAxis function), and moves it using the AddForce function.

It also introduces a tilt to the spaceship as the spaceship moves left or right. The Quaternion.Euler function is used for the rotation of the spaceship depending on the force acting on the Rigidbody. Both the rotation and AddForce methods make use of a Vector3 object for taking information on the direction where the movement should take place.

We have introduced logic that if the player touches an object with the tag Obstacles, the Destroy function destroys the spaceship and the object with which it collides and then You Lose! text is displayed. If it touches an object with the tag Win, it displays You Win!. The latter object is the last wall of the maze.

Now, going back to the game window, you’ll notice that the scripts have been attached to the respective objects.

You’ll now need to create text objects, which is possible by selecting GameObject > UI > Text. Position the text accordingly. Now click on the Player Object and have a look at the settings under the script section.

There should be options like Lose Text and Win Text. Assign the empty fields the appropriate objects by dragging the right game objects from under the hierarchy in the left pane to the empty fields. Setting Speed 8.5 and Tilt 4 worked well for me. Similarly, add appropriate options to the Camera object. You can now test the game by clicking on the Play icon on the top (center) of the window.

Compiling and Building a Standalone

Your brand new spaceship game is now constructed. Save the scene by clicking on File > Save Scene as…. Now select File > Build Settings. Select your operating system and click on the Build button. Your screen should look like this:

Building Standalone

This will create a standalone file for your operating system. Your game is now ready to be played and distributed. Here’s a video of the gameplay:

As you see at the end of the video, I put a huge helicopter in the game – from the Asset Store – and added the Win tag to it. In this way, if my spaceship reaches the helicopter, the game ends – and you’re a winner!

Conclusion

Unity 5 provides a great opportunity for interested people to develop 2D as well as 3D games. As we’ve seen, the game development workflow – including the scripting – is quite elementary, and you can easily develop games of varying complexity using the same workflow.

An example of a game that you could publish and sell would be a spaceship game where enemies fire at you and you fire back at them. The game might consist of several levels or stages.

The learning curve for developing professional games for Unity is not very steep. The Unity Asset Store is a great resource for finding interesting assets for your game, so that you don’t have to start working on your new game from scratch.

Have you developed any games with the help of Unity? What is your favorite gaming engine?