SlideShare a Scribd company logo
1 of 33
The KODERUNNERS Community
• Unity Scripting 101
Introduction to Scripting in Unity using C#
By : Soumik Rakshit
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
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
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
The KODERUNNERS Community
Creative Coding 101 - Abhik Biswas
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
The KODERUNNERS Community
Game Development 101 - Soumik Rakshit
HAPPY CODING & HAPPY GAMING

More Related Content

What's hot

Galactic Wars XNA Game
Galactic Wars XNA GameGalactic Wars XNA Game
Galactic Wars XNA Game
Sohil Gupta
 
How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3d
Dao Tung
 
Task two workflow by tom crook
Task two workflow by tom crookTask two workflow by tom crook
Task two workflow by tom crook
TomCrook
 

What's hot (20)

2D game workflow
2D game workflow2D game workflow
2D game workflow
 
Introduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameIntroduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First Game
 
Galactic Wars XNA Game
Galactic Wars XNA GameGalactic Wars XNA Game
Galactic Wars XNA Game
 
2d game printscreens
2d game printscreens2d game printscreens
2d game printscreens
 
25 tips to build Tutorials on Board Game Arena
25 tips to build Tutorials on Board Game Arena25 tips to build Tutorials on Board Game Arena
25 tips to build Tutorials on Board Game Arena
 
Cameron McRae - 2D Game Workflow
Cameron McRae - 2D Game WorkflowCameron McRae - 2D Game Workflow
Cameron McRae - 2D Game Workflow
 
BGA Studio - Focus on BGA Game state machine
BGA Studio - Focus on BGA Game state machine BGA Studio - Focus on BGA Game state machine
BGA Studio - Focus on BGA Game state machine
 
Manual de usuario de virtualbox
Manual de  usuario de virtualboxManual de  usuario de virtualbox
Manual de usuario de virtualbox
 
How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3d
 
Task two workflow by tom crook
Task two workflow by tom crookTask two workflow by tom crook
Task two workflow by tom crook
 
Keylight ae user guide
Keylight ae user guideKeylight ae user guide
Keylight ae user guide
 
Hackathon 2013 - The Art Of Cheating In Games
Hackathon 2013 - The Art Of Cheating In GamesHackathon 2013 - The Art Of Cheating In Games
Hackathon 2013 - The Art Of Cheating In Games
 
Presentación Unity
Presentación UnityPresentación Unity
Presentación Unity
 
Game maker walkthrough
Game maker walkthroughGame maker walkthrough
Game maker walkthrough
 
Programmers guide
Programmers guideProgrammers guide
Programmers guide
 
unity basics
unity basicsunity basics
unity basics
 
L10 Using Frameworks
L10 Using FrameworksL10 Using Frameworks
L10 Using Frameworks
 
Workflow
WorkflowWorkflow
Workflow
 
Documenting game (recovered)2
Documenting game (recovered)2Documenting game (recovered)2
Documenting game (recovered)2
 
Making My Game
Making My Game Making My Game
Making My Game
 

Similar to Game Development Session - 3 | Introduction to Unity

Chapt 6 game testing and publishing
Chapt 6   game testing and publishingChapt 6   game testing and publishing
Chapt 6 game testing and publishing
Muhd Basheer
 
2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial
tutorialsruby
 
2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial
tutorialsruby
 
Fps tutorial 1
Fps tutorial 1Fps tutorial 1
Fps tutorial 1
unityshare
 
351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf
351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf
351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf
kalelboss
 
Game programming workshop
Game programming workshopGame programming workshop
Game programming workshop
narigadu
 

Similar to Game Development Session - 3 | Introduction to Unity (20)

Pong
PongPong
Pong
 
Chapt 6 game testing and publishing
Chapt 6   game testing and publishingChapt 6   game testing and publishing
Chapt 6 game testing and publishing
 
Unity3d scripting tutorial
Unity3d scripting tutorialUnity3d scripting tutorial
Unity3d scripting tutorial
 
Unity workshop
Unity workshopUnity workshop
Unity workshop
 
Unity - Essentials of Programming in Unity
Unity - Essentials of Programming in UnityUnity - Essentials of Programming in Unity
Unity - Essentials of Programming in Unity
 
DSC RNGPIT - Getting Started with Game Development Day 1
DSC RNGPIT - Getting Started with Game Development Day 1DSC RNGPIT - Getting Started with Game Development Day 1
DSC RNGPIT - Getting Started with Game Development Day 1
 
Game Programming I - Introduction
Game Programming I - IntroductionGame Programming I - Introduction
Game Programming I - Introduction
 
Lecture 1 Introduction to VR Programming
Lecture 1 Introduction to VR ProgrammingLecture 1 Introduction to VR Programming
Lecture 1 Introduction to VR Programming
 
Game Development with AndEngine
Game Development with AndEngineGame Development with AndEngine
Game Development with AndEngine
 
Street runner final
Street runner finalStreet runner final
Street runner final
 
Unity 3d scripting tutorial
Unity 3d scripting tutorialUnity 3d scripting tutorial
Unity 3d scripting tutorial
 
2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial
 
2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial
 
Fps tutorial 1
Fps tutorial 1Fps tutorial 1
Fps tutorial 1
 
351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf
351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf
351111888-Commodore-64-Assembly-Language-Arcade-Programming-pdf.pdf
 
Game programming workshop
Game programming workshopGame programming workshop
Game programming workshop
 
Android game development
Android game developmentAndroid game development
Android game development
 
Introduction to Mobile Game Programming with Cocos2d-JS
Introduction to Mobile Game Programming with Cocos2d-JSIntroduction to Mobile Game Programming with Cocos2d-JS
Introduction to Mobile Game Programming with Cocos2d-JS
 
Easy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkeyEasy coding a multi device game with FireMonkey
Easy coding a multi device game with FireMonkey
 
WP7 HUB_XNA
WP7 HUB_XNAWP7 HUB_XNA
WP7 HUB_XNA
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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
  • 3. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 6. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 9. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 10. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 12. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 13. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 15. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 18. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 19. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 21. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 23. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 24. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 28. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 30. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 31. The KODERUNNERS Community Creative Coding 101 - Abhik Biswas
  • 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
  • 33. The KODERUNNERS Community Game Development 101 - Soumik Rakshit HAPPY CODING & HAPPY GAMING

Editor's Notes

  1. Hands on : Creative Coding ; Perlin Noise : MineCraft , Data Analysis : Bar Graph,