Roller Ball Tutorial Unity3D

Well this is going back to basics…..again!!
Knowing myself the way I do, this is an important step. If I can get the basics right, then I have a good chance of building a good scripting knowledge foundation. In order for a foundation to work properly it must be solid and stable, which is where I want to get my scripting knowledge to. I am going to follow these basic tutorials to get the games built, once they are functioning as they should, then I can go back and add my special designer touches with all my own uniqueness and weirdness. These tutorials will be quite simple but I am hoping to not only enforce and ram into my head further, the lessons I have already learnt, but as well as hopefully pick up some new tidbits and other new information. This tutorial is from the Unity website and the link is as follows: Roller Ball Tutorial
Creating the Playing Field
Iobjectandplane created a plane and a sphere to be used as the ground and the player using the create 3D object under the menu. I reset the components and adjusted the sphere so that it rested on the surface of the plane created. Now I want to write a PlayerController script to be able to move the sphere around. Upon creating the script I found out a new thing.
Well it’s not new, I do recall seeing this before, but what I realise now is that this will actually tell you, show you what and how it should be written.
codehelpNow this is one of those ‘lightbulb’ moments peeps! But also a moment when I feel like smacking myself in the head and going ‘derrrrrrr!’
Oh well better late than never I spose, if I put a somewhat pathetic positive spin on it.  This is what happens when you build up an unhealthy irrational fear of something, it can really mess up things. I am so glad that with this life experience I do understand that even if we have fears, we should also make every attempt to face our fears wherever possible and if safe to do so. Anyways I digress…….

Making the Player Object Move

codetomoveballOk so I have written code to make the ball move, but it dose so at a very slow rate and it takes a while for it to slow down and change direction when you want it to so I have to add to the script something to control the speed of the ball.
controllingthespeedNow in the inspector in Unity you can see in the Player Controller script there is now a speed variable that can be adjusted.
speedvariable_in_inspectorNow I changed the speed to 10 and that gave it a nice feel and made the sphere move at a speed that was easy to control.
Now I jumped the gun a bit and decided to muck around with the design and the look of what I have already created.
I went and created a new folder called ‘Imported’ and then copied two pictures from my  photos. I right -clicked on the ‘Imported’ folder and selected ‘Open in Explorer’. Once open I pasted the pictures in the folder and then closed the explorer. I then simply clicked on the imported picture and dragged it onto the scene, over the top of the plane. This then changed to plane to that picture. I did the same with the sphere.

I have attached a screen shot below.

Playing field is just a photo of the baking tray covered with Alfoil and sprinkled with oil, that I was using to make honey, soy, garlic chicken. Hope it works out!

Attaching the Camera to the Player Object – Sphere

I now have to fix the camera, so that it follows the sphere. I first tried to just drag the main camera and make it a child of the player, but all this did was made the camera spin wildly when I tested it. I then made the player a child of the camera, but this did not change anything. So I have to change how the camera works, to get it to follow the sphere using a script.
So after clicking on the Main Camera I then added component – New Script.
Inspector_add_player_to_cameraOnce the CameraController script was saved and attached to Unity, I opened Unity and in the Inspector on the Main Camera a new box appeared under the CamerController script component labelled ‘Player’ I dragged the player from the Hierarchy into that box. When I tested it the camera followed the sphere even when the sphere went off the edge.
My next step is to set up the playing area so that the sphere cannot fall off the edge, as well as adding some objects as obstacles and pickups.

Adding Walls and Pick Ups
I created an Empty Game Object and renamed it walls. I then created a cube whilst having the Empty GameObject Walls in the hierarchy highlighted. This made the Walls a parent of the new cube created. I once again couldn’t resist and added another photo, which Unity is so cool and turned it automatically into a material which I could drag onto the wall to change its appearance. I moved the wall to the edge of the playing field by dragging it but this was not accurate enough so I changed the position using the X position and changing it to -10. I then duplicated the wall and changed its name to East Wall, changed it’s X position to 10 and it moved to the opposite edge of the playing field. I duplicated the wall again and renamed it North Wall. I didn’t rotate or move the wall but instead re-scaled it to X:20.5, Y:2, Z:0.5 and the changed Z position to 10. For the final wall the South Wall I changed the Z position to -10 so that it moved to its correct position at the south edge of the playing field. Like thus…..
As the walls have Box Collider components the sphere does not go through the walls but is blocked and prevented from falling of the edge..Excellent (Monty Burns style).
Now to create some collectible objects. I created a new cube in the hierarchy and renamed it PickUp. To make it distinguishable to the player, I changed its scale so that it appears to be floating on the playing field as well as rotated it on all the axis and of course added my own particular flair to it.
Now I want this cylinder to rotate and to do this I had to create a new script called Rotator. Its worth noting that when a new script is created it creates the script in the main assets folder. Remember to move the scripts to the created scripts folder as this is good practice and makes it easier to find.

This made the PickUp spin on all axis at a set rate. Now as I am going to have a number of these PickUps I need to turn it into a Prefab. A Prefab is an asset that contains a template of a game object. This enables us to make changes to the Prefab that will affect all other objects that are the same. This is easily done by creating a Prefab folder in the Assets folder and then drag the PickUp into that folder, this instantly creates a Prefab.
I also need to create an empty game object and rename it Pick Up, I then drag the PickUp object into the empty game object called Pick Up so it becomes a child of that empty game object. As per the diagram……
ParentingI can now duplicate this Pick Up and place around the playing field, I did this until I had 8 Pick Ups dotted around the playing field.
PickUps _placed


At this stage I decided that the Pick Ups didn’t stand out enough so IPlaying_field_with_objects changed the material used by importing another picture.Pickup and then
dragging the picture onto the first Pick Up child. I must then select Apply in the Pick Ups inspector to make sure the change applies to all the Pick Ups. I also went through and changed the walls.

Ok, we’re getting there.

Making the Pick Ups function

Now I want to make the Pick Ups actually work as pick ups, so that when the sphere hits them they will not only disappear, but also award points. To do this I have to muck around with the colliders attached to each object such as the walls, playing field, Pick Ups etc. We must test each collider so that they know what to do when the sphere comes into contact with that object. Does it disappear? Does it change?
To change the sphere’s collider and what it does, I have to go to the Player Controller Script and add the conditions for when colliders touch:

void OnTriggerEnter(Collider other);

Creating and adding Tag

if (other.gameObject.CompareTag(“Pick Up”))
other.gameObject.SetActive (false);

Selecting Is Trigger

I selected the Pick Up Prefab and  then had to create a tag called Pick Up in the inspector and then select it. All the Pick Ups now have the tag Pick Up. When I tested the game, the sphere did not pick up the Pick Ups but bounced off them because there was no trigger
set. Once I selected that box and tested the game the sphere picked up the Pick Ups, which disappeared once hit.
Excellent (Monty Burns style..)
I have to add a rigid body to the Pick Up Prefab as it uses up extra memory calculating

Selecting is Kinematic

stuff (static collider), which is not that important for this game, but can be very important in a bigger more intricate game where every calculation done burns up precious time and memory resources. Adding a rigid body turns them into dynamic colliders which do not use up as much processor power calculating stuff it doesn’t
need to recalculate. Once you add a rigid body to the Pick Ups, they will be subject to gravity in the engine and will simply fall through the floor. To prevent this we select the ‘Is Kinematic’. A Kinematic rigid body will not react to physics and can be moved by its Transform.
I once again couldn’t help it but add some designer flair.

Adding Obstacles

Obstacles_addedBy pretty much following the steps I have already done to create the Pick Ups, I added some obstacles as well as extra Pick Ups. The obstacles spin counter-clockwise on the y-axis only and the material was done the same as before with copying a picture into the import file and applying it to the obstacle.

Adding a Pick Up Tracker UI

Now I add a Pick Up count tracker. To do this I need to go to the Player Controller Script and create a tool for storing this value of how many Pick Ups have been picked up. In the script I create an int called count and set this to 0 and then down in the onTrigger collider section of the script, I add 1 to the count.

Creating an int called count and setting it to 0.
Adding 1 to count after game object Pick Up has been picked up.

So this has given me a tool to keep track and the amount of Pick Ups picked up. To Add_Textdisplay this information I need to go to the hierarchy and create a new UI – Text.
This also creates a Canvas and an Event Manager, which for now is not important to know how they work but suffice to know that they are important and are required to display the text. I changed the Text to Count Text.
Text_positioningWhen you highlight Count Text and look in the inspector you will not see a normal Transform as in other objects but instead will see a Rect Transform, which is used to set the position of the text display and keep it anchored in that position. I changed the colour of the text as well as downloaded a Free Font pack from the Assets store and applied it to the text. I also changed the size and spacing from the edge of the game screen by changing the X and Y axis in the Rect Transform to 10 and -10 respectively.

Now to connect up the count text with the actual Pick Up pick ups. To do this I open the Using_Unity_UIPlayer Controller script again and add ‘using UnityEngine.UI;’ at the top of the script to let the script know that it is interacting with Unity’s UI system. I then create a public Text variable to hold reference to the Text UI.
I then set the the starting value of the UI text’s property but instead of writing the same piece of code twice I need to create a function that we can call upon to display the count.
I then called that function in the Start and when a Pick Up was picked up.Calling_function
Once you have saved the script and returned to Unity to get the Player to interact with the Count you drag the Count Text over to the Win Text box in the Player Controller component in the inspector..
And wallah!! You get the following on screen..
Displaying a Win Message

Once all the Pick Ups have been picked up I then have to display a message letting the player know.
I did this the same way as I added my Count by creating a UI Text. This adds the new text to the current canvas. I changed the new text to Win Text and adjusted the colour, font, position and size in the inspector.
Win_text.PNGNow I only want this text to display when all the Pick Ups have been picked up so I added a variable called winText and then set its value at the Start so that it does not display.
creating_wintext_variableI then in the setText function I added a command to display a message once all Pick Ups (10) were picked up.
winmessageSave script and return to Unity and drag the Win Text over to the Player Controller script Win Text box component as above with the Count Text and save and test.
The Win Text has the wrong colour so I changed it to something that stands out more, as well as changed the position.
I didn’t like how the background of the playing field looked, ie the skybox so I changed that. I downloaded a package from the assets store (Free HDR Sky by Proassets). I then simply chose the skybox I wanted and dragged it onto the scene over the background area and released it. The skybox is now changed.
Adding SFX and Music

There are no sounds, and having no sound never made a game interesting. So what I did was in the hierarchy I created a new Audio – Audio Source and renamed it Start Music. I then downloaded a little jingle from called Piano Improvisation created by newagesoup. I then imported that into the SFX folder created. Once imported I selected Start Music in the hierarchy and in the inspector under the Audio Clip box I clicked on the little circle and selected the music I imported and then ticked the Loop box.. Now when I test the game, the music plays and loops until I stop the test. I want to make this so that the music plays when I start the level an finishes once I get all the Pick Ups.

Making Extra Levels
Before I created any new levels, I first made an empty game object and called it ‘Playing Field’, I then made the walls and the ground a child of this game object and then dragged it into the Prefab section so that I would not have to build a new playing field each time I created a new level.

Adding a Timer

I want to add a timer so that the player can see how fast they can pick up all the Pick Ups. I had to do a bit of research on this as it is completely new to me, although I do recall vaguely in Trimester 1 seeing something about this, but that’s a faded memory filled with many blanks I am sure.

I am going to stop this blog here, as it is a helluva lot of info to digest. Truth be told it is not really a dev diary so to speak but rather a tutorial itself for anyone trying to build the game.  I have received a lot of benefits from going through this process as I learnt a lot more about scripting as well as Unity itself, particularly the website and online help tools that exist. That bit of learning itself is probably one of the most valuable and I consider it a “give a man a fish and he’ll eat for a day, teach man to fish and he’ll eat for a lifetime.”
12725133_558791800966185_1404713360_nI will post updates on how I worked out the timer as well as a few other things that I have added through my designer touch. Until then, thank you for your company.

God’s blessings upon you.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s