Book Image

Unity 2017 Mobile Game Development

By : John P. Doran
Book Image

Unity 2017 Mobile Game Development

By: John P. Doran

Overview of this book

Unity has established itself as an overpowering force for developing mobile games. If you love mobile games and want to learn how to make them but have no idea where to begin, then this book is just what you need. This book takes a clear, step-by-step approach to building an endless runner game using Unity with plenty of examples on how to create a game that is uniquely your own. Starting from scratch, you will build, set up, and deploy a simple game to a mobile device. You will learn to add touch gestures and design UI elements that can be used in both landscape and portrait mode at different resolutions. You will explore the best ways to monetize your game projects using Unity Ads and in-app purchases before you share your game information on social networks. Next, using Unity’s analytics tools you will be able to make your game better by gaining insights into how players like and use your game. Finally, you’ll learn how to publish your game on the iOS and Android App Stores for the world to see and play along.
Table of Contents (11 chapters)

Creating the player

Now that we have Unity opened up, we can actually start building our project. To get started, let's build a player that will always move forward. Let's start with that now:

  1. Let's create some ground for our player to walk on. To do that, let's go to the top menu and select GameObject | 3D Object | Cube.
  2. From there, let's move over to the Inspector window and change the name of the object to Floor. Then, on the Transform component, set the Position to (0, 0, 0), which we can either type in, or we can right-click on the Transform component and then select the Reset Position option.
  3. Then, we will set the Scale to (7, 0.1, 10):

In Unity, by default, 1 unit of space in Unity is representative of 1 meter in real life. This will make the floor longer than it is wide (X and Z), and we have some size on the ground (Y), so the player will collide and land on it because we have a Box Collider component attached to it.

  1. Next, we will create our player, which will be a sphere. To do this, we will go to Game Object | 3D Object | Sphere.
  1. Rename the sphere to Player and set the Transform component's Position to (0, 1, -4):

This will place the ball slightly above the ground, and shifts it back to near the starting point. Note that the camera object (you can see a camera icon to point it out) is pointing toward the ball by default because it is positioned at 0, 1, -10.

  1. We want the ball to move, so we will need to tell the physics engine that we want to have this object react to forces, so we will need to add a Rigidbody component. To do so, go to the menu and select Component | Physics | Rigidbody. To see what happens now, let's click on the Play button that can be seen in the middle of the top toolbar:

As you can see in the preceding screenshot, you should see the ball fall down onto the ground when we play the game.

You can disable/enable having the Game tab take the entire screen when being played by clicking on the Maximize On Play button at the top, or by right-clicking on the Game tab and then selecting Maximize.
  1. Click on the Play button again to turn the game off and go back to the Scene tab, if it doesn't happen automatically.

We want to have the player move, so in order to do that, we will create our own piece of functionality in a script, effectively creating our own custom component in the process.

  1. To create a script, we will go to the Project window and select Create | Folder on the top-left corner of the menu. From there, we'll name this folder Scripts. It's always a good idea to organize our projects, so this will help with that.
If you happen to misspell the name, go ahead and select the object and then single-click on the name and it'll let you rename it.
  1. Double-click on the folder to enter it, and now you can create a script by going to Create | C# Script and renaming this to PlayerBehaviour (no spaces).
The reason I'm using the "behaviour", "spelling" instead of "behavior" is that all components in Unity are children of another class called MonoBehaviour, and I'm following Unity's lead in that regard.
  1. Double-click on the script to open up the script editor (IDE) of your choice and add the following code to it:
using UnityEngine; 

public class PlayerBehaviour : MonoBehaviour
// A reference to the Rigidbody component
private Rigidbody rb;

// How fast the ball moves left/right
public float dodgeSpeed = 5;

// How fast the ball moves forwards automatically
public float rollSpeed = 5;

// Use this for initialization
void Start ()
// Get access to our Rigidbody component
rb = GetComponent<Rigidbody>();

// Update is called once per frame
void Update ()
// Check if we're moving to the side
var horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;
rb.AddForce(horizontalSpeed, 0, rollSpeed);

In the preceding code, we have a couple of variables that we will be working with. The rb variable is a reference to the game object's Rigidbody component that we added previously. It gives us the ability to make the object move, which we will use in the Update function. We also have two variables dodgeSpeed and rollSpeed, which dictates how quickly the player will move when moving left/right, or when moving forward, respectively.

Since our object has only one Rigidbody component, we assign rb once in the Start function, which is called when the game starts, as long as the game object attached to this script is enabled.

Then, we use the Update function to check whether our player is pressing keys to move left or right as based on Unity's Input Manager system. By default, the Input.GetAxis function will return to us a negative value moving to -1 if we press A or the left arrow. If we press the right arrow or D, we will get a positive value up to 1 returned to us, and the input will move toward a 0 if nothing is pressed. We then multiply this by dodgeSpeed in order to increase the speed so that it is easier to be seen.

For more information on the Input Manager, check out

Finally, once we have that value, we will apply a force to our ball's horizontalSpeed units on the X-axis and rollSpeed in the Z-axis.

  1. Save your script, and return to Unity.
  1. We will now need to assign this script to our player by selecting the Player object in the Hierarchy window, and then in the Inspector window, drag and drop the PlayerBehaviour script from the Project window on top of the Player object. If all goes well, we should see the script appear on our object, as follows:

Note that when writing scripts if we declare a variable as public, it will show up in the Inspector window for us to be able to set it. We typically set a variable as public when we want designers to tweak the values for gameplay purposes.

  1. Save your scene by going to File | Save Scene. Create a new folder called Scenes and save your scene as Gameplay. Afterward, play the game and use the left and right arrows to see the player moving according to your input, but no matter what, moving forward by default: