Sunday, 10 January 2016

Simple ball game with Unity 5

            To begin with what we are going to create now is a ball rolling on a surface ; that is it! This is a simple introduction towards working with Unity 5 for beginners..(I am not a pro myself). I will try my best make things easy to follow. Lets start. This is the final product..(for me, yours might be better)



Prerequisites:
1. You need to download Unity from their website. You also need a unity account. 

2. Download android sdk from here.

3. Download Standard Assets From the Unity store.

4. Little knowledge of c# and JavaScript.

5. Patience..(Most Important)

Project Information:

  • Difficulty: Beginner
  • Unity version: 5.3.1
  • Date:10/1/15

Step 1:
Install unity and android sdk to your preferred location. The location does not matter as once the location of sdk has been fed to unity you can forget about it.

Step 2:
Open Unity and it will ask you to name your new project and a save location and then click on create project. Within the program  you will see a scene tab, inspector 
tab, hierarchy tab, project tab etc. 
The scene tab holds the actual play area. On the top of the screen there are play and pause buttons to start the game in the editor. The screenshot shows the unity elements.




Step 3:
In the hierarchy tab, right click and create a 3D object>cube.
A new cube is spawned at location (0,0,0)
Hold middle mouse button the pan, Left click to select and right click to look around.




Step 4:
We are going to use this cube as a surface for the ball to move around. To use this as a plane we need to increase its size in 2 of the axes. In the inspector element,there is a transform component. Here we need to scale the cube by 10 in X and Z fields.




Step 5:
 In Unity 5 controlling the ball is easier. In previous versions it was all done with scripting. Import the characters and CrossPlatformInput packages from Assets>Import package.

Step 6:
 Drag the RollerBall prefab in the project tab. The Prefab allows you to create new object instances. It saves time
thats all you need to know right now. The prefab will most likely spawn at (0,0,0) position. The surface and the ball are intersecting so you need to bring the ball up by increasing its Y axis.

Step 7:
Now you need to attach the Main camera to the ball. To do this you can make the camera a child of the player object(ball) but it will roll the camera around. So instead use the following script in c# to attach the camera to the ball.


using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

    public GameObject player;

    private Vector3 offset;

    void Start ()
    {
        offset = transform.position - player.transform.position;
    }
    
    void LateUpdate ()
    {
        transform.position = player.transform.position + offset;
    }
}

Step 8:
To do this right click on project tab and create new c# script rename it to playercontroller and open it. It will open in monodevelop.
Add this component to main camera and set player as Rollerball.




Step 9:
You can create walls by reshaping cubes so that the player does not fall off the map. You can add small details like positioning the camera at an angle or adding materials to objects but lets move on to converting the game to android platform.




Step 10:
Go to files>Build settings. Click on android and switch platform. Go to player settings and give company names and product name and a logo. Go to CrossPlatformInput in project tab and copy the MobileSingleStickControl prefab to the Hierarchy.




Step 11:
Now you just have to build the game from Files>Build settings>Build. Copy the game to your phone and install it.
There you have it, a simple ball roll game which does nothing... yet!


Stay tuned for future posts...Good luck