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

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 GameSarah Sexton
 
Galactic Wars XNA Game
Galactic Wars XNA GameGalactic Wars XNA Game
Galactic Wars XNA GameSohil Gupta
 
2d game printscreens
2d game printscreens2d game printscreens
2d game printscreensElliot Black
 
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 ArenaBoard Game Arena
 
Cameron McRae - 2D Game Workflow
Cameron McRae - 2D Game WorkflowCameron McRae - 2D Game Workflow
Cameron McRae - 2D Game WorkflowCameronMcRae901
 
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 Board Game Arena
 
Manual de usuario de virtualbox
Manual de  usuario de virtualboxManual de  usuario de virtualbox
Manual de usuario de virtualboxRuben A Lozada S
 
How tomakea gameinunity3d
How tomakea gameinunity3dHow tomakea gameinunity3d
How tomakea gameinunity3dDao Tung
 
Task two workflow by tom crook
Task two workflow by tom crookTask two workflow by tom crook
Task two workflow by tom crookTomCrook
 
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 GamesSouhail Hammou
 
Game maker walkthrough
Game maker walkthroughGame maker walkthrough
Game maker walkthroughLewisB2013
 
Documenting game (recovered)2
Documenting game (recovered)2Documenting game (recovered)2
Documenting game (recovered)2BenWhite101
 
Making My Game
Making My Game Making My Game
Making My Game terry96
 

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 publishingMuhd Basheer
 
Unity3d scripting tutorial
Unity3d scripting tutorialUnity3d scripting tutorial
Unity3d scripting tutorialhungnttg
 
Unity - Essentials of Programming in Unity
Unity - Essentials of Programming in UnityUnity - Essentials of Programming in Unity
Unity - Essentials of Programming in UnityNexusEdgesupport
 
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 1DeepMevada1
 
Game Programming I - Introduction
Game Programming I - IntroductionGame Programming I - Introduction
Game Programming I - IntroductionFrancis Seriña
 
Lecture 1 Introduction to VR Programming
Lecture 1 Introduction to VR ProgrammingLecture 1 Introduction to VR Programming
Lecture 1 Introduction to VR ProgrammingKobkrit Viriyayudhakorn
 
Game Development with AndEngine
Game Development with AndEngineGame Development with AndEngine
Game Development with AndEngineDaniela Da Cruz
 
2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorialtutorialsruby
 
2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorial2%20-%20Scripting%20Tutorial
2%20-%20Scripting%20Tutorialtutorialsruby
 
Fps tutorial 1
Fps tutorial 1Fps tutorial 1
Fps tutorial 1unityshare
 
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.pdfkalelboss
 
Game programming workshop
Game programming workshopGame programming workshop
Game programming workshopnarigadu
 
Android game development
Android game developmentAndroid game development
Android game developmentdmontagni
 
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-JSTroy Miles
 
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 FireMonkeypprem
 

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

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

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,