Game Development Session - 3 | Introduction to Unity
1. The KODERUNNERS Community
• Unity Scripting 101
Introduction to Scripting in Unity using C#
By : Soumik Rakshit
2. The KODERUNNERS Community
Be Sure to Match the instructions with the
screenshot on the next page.
If you have set up a project in Unity, you will get an interface which looks something like this
irrespective of the version you are using.
In the hierarchy panel, there are 2 game objects by default - Main Camea and Directional Light.
If you click on any game object, their properties are shown on the isnspector panel.
The game as seen from the eyes of the main camera is displayed in the game view panel.
Other than these we also have the Project and Console Panel.
Game Development 101 - Soumik Rakshit
4. The KODERUNNERS Community
Our Objective
Our objective is to create a console based game which helps us to digest the concepts of Unity C#.
We won't be using any kind of Graphics for the current game.
The game primarily shows the player a random time limt between 5 and 21 seconds and starts
countdown to that time.
The game asks us to hit space when we think we are close to that time.
Finally it shows the player how accurate he/she was to guess the correct time.
Game Development 101 - Soumik Rakshit
5. The KODERUNNERS Community
Starting with the Game.....
First of all you need to create an empty game object.
Press ctrl+shift+n to create an empty gameObject.
You will see that the empty gameObject has been created.
The purpose of the empty gameObject is to hold the script that we are writing.
Game Development 101 - Soumik Rakshit
7. The KODERUNNERS Community
Remember
The concept of Unity is just like shooting a movie.
In a movie the director directs the actors according to the scripts written by the scriptwriter in front
of the camera.
In Unity the gamObjects(instead of actual actors) behave according to the scripts you write(that
makes you the scriptwriter and Unity itself the director) in front of a camera.
The empty gameObject will behave according to the script we attach to it.
We are not involving any camerawork this time, so our game will be played on the Unity Console
Panel.
Game Development 101 - Soumik Rakshit
8. The KODERUNNERS Community
Starting off the script
First in your project panel, open the Assets folder.
We will be creating our script inside the Assets folder.
Right click , then select Create and finally select C# script. Name the empty script TimeGame(or
any other fancy name you like).
Game Development 101 - Soumik Rakshit
11. The KODERUNNERS Community
Adding the Script to the GameObject
When you select the empty GameObject, you can see its properties on the Inspector Panel
Under its properties, there is a button called Add Component.
Go to Add Component, then Scripts, then TimeGame(or whatever name you have given to the
script).
Game Development 101 - Soumik Rakshit
14. The KODERUNNERS Community
Opening the Script
Double Click on The script to open it.
Now the script opens using the default code editor which is there with your version. If you are using
Unity 5.6, then most probably it is Visual Studio.
For the tutorials I have used MonoDevelop. If you have any issues regarding the code editor
please let me know.
The default code looks something like the one showed in the next screenshot.
Game Development 101 - Soumik Rakshit
16. The KODERUNNERS Community
Similar to Processing
The code structure of Unity C# is exactly similar to p5.js or Processing Java or Processing.py
The function void Start() is called initially at the beginning of the game. Hence it can be used for
setting up the initial conditions and initialization of variables and objects.
The function void Update() is called once per frame.
Note: By default frame rate might be 24 or 30 fps depending on the version of Unity. It means that
the Update function is called 24-30 times per secon depending on the version.
Game Development 101 - Soumik Rakshit
17. The KODERUNNERS Community
The print() function
To print anything on the console, we use the print() function.
At first we use it to print out the initial message.
We ask the player to hit the spacebar when he thinks it is the time.
Save the script and run it.
Note: In order to run the game, use the play, pause and stop buttons. To run the game, hit the play
button and to stop the game, hit the play button again.
Note: You cannot run a single script in a game. When you hit the play button, you run the game as
a whole. It is not an issue at present since we will be dealing with a single script.
Game Development 101 - Soumik Rakshit
20. The KODERUNNERS Community
Setting the Random Time
For setting the random time we create a function void setRandomTime().
For setting the random time, we use the Range function under the Random class.
The Range function takes two parameters - lower range(inclusive) and upper range(exclusive).
Hence to generate a random value from 5 to 20, we write the following code: waitTime =
Random.Range (5, 21);
Note: Be sure to declare waitTime globally as an integer variable. Just write in the global scope: int
waitTime;
Note: Any uninitialized global variable is automatically initialized to zero.
Game Development 101 - Soumik Rakshit
22. The KODERUNNERS Community
Setting the Start Time
We set the start time as the current time.
This can be done with the member variable time which is a member of the Time class. Time.time
stores the current clock time in seconds.
We just store Time.time in a variable roundStartTime in the Start() function and then print it out.
Note: Be sure to declare roundStartTime globally as a float variable.
Note: We should also call the setRandomTime() function in the Start() function. You can also print
the waitTime value within the setRandomTime() method.
Note: If you run the code at this point, you might get a warning message that you have not used
the roundStartTime variable. Ignore it and run the code.
Game Development 101 - Soumik Rakshit
25. The KODERUNNERS Community
Summing up Initialization
This far we have set the initial conditions.
We have set the player wait time (waitTime) to a random value in the range 5-20.
We have set the start time (roundStartTime) to the current clock time before calling the update
function.
PS: You can print and check out the value of the roundStartTime variable, but we do not need to do
so in the actual game.
Game Development 101 - Soumik Rakshit
26. The KODERUNNERS Community
Checking keyboard input
In the Update() function we check whether the the player has hit the spacebar or not.
To do so we check the value returned by GetKeyDown() which is a boolean type member method
of the Input class. It checks whether a certain key has been pressed down or not.
The required value should be KeyCode.Space.
Hence we write the following code: if (Input.GetKeyDown (KeyCode.Space)){ }
Game Development 101 - Soumik Rakshit
27. The KODERUNNERS Community
Creating the mechanics
Now we move on to the actual game mechanncs. Game mechanncs is that part of the code which
contains the functional aspects of the code.
Inside the if block(i.e, when the player has hit the spacebar). We can get the time the player has
waited to before hitting the spacebar.
We can do this by the subtracting the roundStartTime from the current time. The following code can
be used: float playerWaitTime= Time.time-roundStartTime;
Game Development 101 - Soumik Rakshit
29. The KODERUNNERS Community
Showing the Time lag
We will be diplaying how much difference is there between the playerWaitTime and waitTime.
To do so we write the following code: float error=Mathf.Abs(waitTime-playerWaitTime);
Now we just print out the result using the print().
Note: Mathf.abs returns the absolute value of a float value.
Note: We need the absolute value becouse the player might hit the spacebar after the alotted time.
Game Development 101 - Soumik Rakshit
32. The KODERUNNERS Community
Assignment
That sums up the basic game mechanics.
Now you could print out different messages to the player based on the error.
In doing so you can use if-else or switch case.
Game Development 101 - Soumik Rakshit