Thursday, 19 May 2016

The Ball Rolling Tutorial in Unity 3D

By the end of this post we'll have made a simple but complete game that can be played in a standalone player! 

If you want to try and figure out what the hell is happening with the 3D Editor by purely playing around on your own without any prior experience or guidance then good luck, it's not at all obvious how to make anything do anything. I think it took me five minutes of serious investigation to create a featureless cube and have it appear in the game camera...

... almost

I did the most obvious thing I could think of and searched for a basic tutorial and the good folks at Unity did not disappoint. There is a complete beginner's tutorial which requires no prerequisites other than that you have installed Unity (check!) and you can find it here.

The Roll a Ball tutorial consists of a series of walkthrough videos on how to make a simple token collecting game . There's an intro video explaining what you can expect to learn. The videos appear to be recorded on a Mac, and using the Pro version of the Editor, but it's mostly easy enough to figure out what's going on.

1-1. Setting up the game  -- This 7 minute video walks you through creating a static sphere on a plane and adding colour to the plane. The tutorial starts getting you used to the editor's interface quite nicely and starts implicitly giving you a sense of how to structure your projects in a sensible way.

1-2. Moving the Player -- In this 15 minute session, we add a physics script to the ball so that it can controlled by keyboard input. The code that is used to make this all work is given to us, and explained line by line. I wouldn't worry at this point if you're not a code whiz. I'm certainly not. Peruse the code as much as you can stomach it for now and add the physics component to the ball.

NB: There are related tutorials in each of these pages, if you want to learn more!

2-1. Moving the Camera -- In this quick 5 minute session, we add another script, but this time to the game's main camera. The main camera's position decides what the player sees during every frame and we would like it to track the position of the player's sphere character. The tutorial provides the script and an explanation for how it is constructed. Again, you can spend as much time learning about the code snippet here as you like, but all you have to do is assign the script to the camera and hit play.

How things should look after we've added walls in part 2-2

2-2. Setting Up the Play Area -- In another brief 5 minute session, we place solid walls around the edges of the play field to stop the ball rolling off. The tutorial teaches us that we can store similar wall objects in within a neat hierarchical structure if we wish, to keep the project organised. Otherwise, we're just creating more cubes!

3-1. Creating Collectible Objects -- We create collectible objects in this part which take the form of stationary rotating cubes. The script for this idle rotation is given to us and explained in detail. The really useful new concept in this video though are prefab assets, which allows multiple instances of the same object to have their properties edited at once. Since we want to have our game littered with collectibles, it makes sense that we'd want to change aspects of all of them at once sometimes, rather than doing it exhaustively for each instance of the collectible. This tutorial teaches you how this works in Unity.

3-2. Collecting the Pick Up Objects -- Those collectible cubes from 3-1 need collecting on collision, much like how Mario's Coin objects disappear on contact in those games. This tutorial delves into how Unity handles collision events and provides many avenues for further study of Unity collisions, including how to make economies of memory resources. We're updating the script governing the player character again so that we can collect cubes but we also need to learn how to use 'tag' all instances of the Pick Up so that we don't end up 'collecting' the floor and walls by mistake.

And voila! We have collectible cubes!
3-3. Displaying the Score and Text -- In this tutorial we want to privately record the number of cubes that we've collected and publicly display a score on the screen for the player. Although the on page script is called DisplayManager, you can copypasta the script here into the PlayerController you've been working with and things will work as they should. I recommend following along with the video to get some intuition as to how the code works. You'd be amazed what you pick up by doing this.  

When we display information to the player such as score, we consider this to be a part of the  'User Interface' (more commonly shortened to 'UI'), and it is Unity's UI system that we will be encountering for the first time in this segment. We add text count the number of cubes collected and also to inform us when all of the cubes have been collected. We now have a simple but complete game which runs in the Unity editor, but we'd like to 'build and deploy' the game so that we can run it in a standalone player and share this mastapeece with our friends. This brings us to...

3-4.  Building the Game -- Finally, this very short end to the tutorial shows us how to build and deploy our game so the can run it like any other application on our computer. You're only a few clicks away from completing your first playable (in my case PC) game using Unity! Wahoooo!

That completes the Roll-a-Ball tutorial for Unity, tune in next time!