SlideShare a Scribd company logo
1 of 16
Download to read offline
Game Development
University of Anbar
Game Development
• Sprite: is an image that can be used as a 2D object. Sprites, by default, are rendered directly against the
camera.
• Textures: graphical components. These are also images, but they are used to change the appearance of an
object in both 2D and 3D. But Textures are not objects. This means that you cannot get them to move during
gameplay.
• Rigidbody: A Rigidbody 2D component places an object under the control of the physics engine.
• There are three options for Body Type which defines the movement (position & rotation) behavior
collider interaction:
• Dynamic
• Kinematic
• Static
• Collider: Collider components define the shape of a GameObject for the purposes of physical
collisions. A collider, which is invisible, does not need to be the exact same shape as the
GameObject.
• The scripting system can detect when collisions occur and initiate actions
• OnCollisionEnter2D: When the collision is detected.
• OnCollisionStay2D: During updates where contact is maintained,
• OnCollisionExit2D indicates that contact has been broken
Simple Project
• Open unity and select new project
• Select 2D from the template and change the project name from the project setting
Simple Project
• Create Sprite (2 of them). I will choose square. Called the first one Player and the second one Ground.
Simple Project
• Change the size of the ground to be rectangle to look like a ground. You can do that by clicking on the
gameObject on the Hierarchy view and and select the Rect Tool from the top bar. Then you can see blue dot
on the corner of the object to change the size.
Simple Project
• Add Box Collider 2D to the ground. Click on the Ground object and from the Inspector view click on the add
Component and select “Box Collider 2D”.
• Select Player object and add “Rigidbody2D” and Capsule Collider 2D from add component on the Inspector
view.
Simple Project
• Create folder of the C# scripts. Click on the Assets from the Project View, right click on the Assets view and
select Create and then Folder, name the folder as Scripts.
• Go inside the Scripts folder (double click) and create script file (right click -> Create -> C# Script) and name it
“PlayerController”.
Simple Project
• After that drag the C# file over the Player gameObject (adding the script to the Player GameObject). Now if
you click on the Player, you can see the script in the inspector view. To edit the code double click on the C#
file which will open it with Visual Studio (VS). To make sure that the code open with VS, go to Edit ->
Preferances -> External Tools and change the “External Tool Editor” to VS
Simple Project
• After adding the C# code, the object will keep jumping. To solve this issue, we can add a collider (Box Collider
2D) to the Player objects lightly below the object to help deciding when to jump. Then we need to check the
isTriger, this will just triger an action without the actuality of player hitting something.
Simple Project
• To avoid the rotation of the Player when moving side, on the Rigidbody2D check option on the Constraints -
>Freeze Rotation.
• Now we need to change the Tag for the objects. Set the Player object with tag “Player” and create new tag
“Platform” for the Ground. Click on the Player object and from the inspector change the tag. To create one,
choose Add Tag and click the + sign and enter the name of the new tag and save. Go back to Ground object
and choose the new created tag
Simple Project
• To control how the player move we can change the Gravity and Linear drag from the Rigidbody2D. Also
setting Collision detection to continuous with prevent the object from going through another object when
there is a collision.
The C# Script
• Define class variable
private Rigidbody2D rigidbody2DObject;
private float moveSpeed;
private float jumpForce;
private float moveHorizantal;
private float moveVertical;
private bool isJumping;
In the start function we need to get the rigid body component and set the values
void Start()
{
rigidbody2DObject = gameObject.GetComponent<Rigidbody2D>();
moveSpeed = 2f;
jumpForce = 60f;
isJumping = false;
}
In the update function we get the values for the horizontal (left/right) and vertical. Then based on
these value the object move.
void Update()
{
moveHorizantal = Input.GetAxisRaw("Horizontal");
moveVertical = Input.GetAxisRaw("Vertical");
if(moveHorizantal > 0 || moveHorizantal < 0)
{ // move the player left or right
rigidbody2DObject.AddForce(new Vector2(moveHorizantal * moveSpeed, 0), ForceMode2D.Impulse);
}
if(!isJumping && moveVertical > 0f)
{ // move the object up
rigidbody2DObject.AddForce(new Vector2(0f, moveVertical*jumpForce),ForceMode2D.Impulse);
}
}
• To detect when to stop jumping we call the collision functions.
void OnTriggerEnter2D(Collider2D collision)
{ // when the collsion start disable the ability to jump
if(collision.gameObject.tag == "Platform")
{
isJumping = false;
}
}
void OnTriggerExit2D(Collider2D collision)
{ // when the player return to the ground enable the player to jump
if (collision.gameObject.tag == "Platform")
{
isJumping = true;
}
}

More Related Content

Similar to Lecture2_practice.pdf

Let's make a game unity
Let's make a game   unityLet's make a game   unity
Let's make a game unitySaija Ketola
 
Cardboard VR: Building Low Cost VR Experiences
Cardboard VR: Building Low Cost VR ExperiencesCardboard VR: Building Low Cost VR Experiences
Cardboard VR: Building Low Cost VR ExperiencesMark Billinghurst
 
Building VR Applications For Google Cardboard
Building VR Applications For Google CardboardBuilding VR Applications For Google Cardboard
Building VR Applications For Google CardboardMark Billinghurst
 
Mobile AR Lecture6 - Introduction to Unity 3D
Mobile AR Lecture6 - Introduction to Unity 3DMobile AR Lecture6 - Introduction to Unity 3D
Mobile AR Lecture6 - Introduction to Unity 3DMark Billinghurst
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android GamesPlatty Soft
 
Unity Google VR Cardboard Deployment on iOS and Android
Unity Google VR Cardboard Deployment on iOS and AndroidUnity Google VR Cardboard Deployment on iOS and Android
Unity Google VR Cardboard Deployment on iOS and AndroidKobkrit Viriyayudhakorn
 
Get Into Sprite Kit
Get Into Sprite KitGet Into Sprite Kit
Get Into Sprite Kitwaynehartman
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろうUnity Technologies Japan K.K.
 
Lecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in UnityLecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in UnityKobkrit Viriyayudhakorn
 
Getting started with Verold and Three.js
Getting started with Verold and Three.jsGetting started with Verold and Three.js
Getting started with Verold and Three.jsVerold
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom componentsJeremy Grancher
 
Sx sw speaker proposal slides
Sx sw speaker proposal slidesSx sw speaker proposal slides
Sx sw speaker proposal slidesMitch Williams
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)noorcon
 
Game development with Cocos2d
Game development with Cocos2dGame development with Cocos2d
Game development with Cocos2dVinsol
 
2 d gameplaytutorial
2 d gameplaytutorial2 d gameplaytutorial
2 d gameplaytutorialunityshare
 
Unity - Building Your First Real-Time 3D Project - All Slides
Unity - Building Your First Real-Time 3D Project - All SlidesUnity - Building Your First Real-Time 3D Project - All Slides
Unity - Building Your First Real-Time 3D Project - All SlidesNexusEdgesupport
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)noorcon
 

Similar to Lecture2_practice.pdf (20)

Let's make a game unity
Let's make a game   unityLet's make a game   unity
Let's make a game unity
 
Cocos2d for beginners
Cocos2d for beginnersCocos2d for beginners
Cocos2d for beginners
 
Cardboard VR: Building Low Cost VR Experiences
Cardboard VR: Building Low Cost VR ExperiencesCardboard VR: Building Low Cost VR Experiences
Cardboard VR: Building Low Cost VR Experiences
 
Cocos2d programming
Cocos2d programmingCocos2d programming
Cocos2d programming
 
Building VR Applications For Google Cardboard
Building VR Applications For Google CardboardBuilding VR Applications For Google Cardboard
Building VR Applications For Google Cardboard
 
Mobile AR Lecture6 - Introduction to Unity 3D
Mobile AR Lecture6 - Introduction to Unity 3DMobile AR Lecture6 - Introduction to Unity 3D
Mobile AR Lecture6 - Introduction to Unity 3D
 
Tools for developing Android Games
 Tools for developing Android Games Tools for developing Android Games
Tools for developing Android Games
 
Unity Google VR Cardboard Deployment on iOS and Android
Unity Google VR Cardboard Deployment on iOS and AndroidUnity Google VR Cardboard Deployment on iOS and Android
Unity Google VR Cardboard Deployment on iOS and Android
 
Get Into Sprite Kit
Get Into Sprite KitGet Into Sprite Kit
Get Into Sprite Kit
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
 
Lecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in UnityLecture 2: C# Programming for VR application in Unity
Lecture 2: C# Programming for VR application in Unity
 
Getting started with Verold and Three.js
Getting started with Verold and Three.jsGetting started with Verold and Three.js
Getting started with Verold and Three.js
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
 
Sx sw speaker proposal slides
Sx sw speaker proposal slidesSx sw speaker proposal slides
Sx sw speaker proposal slides
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 3 (Preview)
 
Game development with Cocos2d
Game development with Cocos2dGame development with Cocos2d
Game development with Cocos2d
 
2 d gameplaytutorial
2 d gameplaytutorial2 d gameplaytutorial
2 d gameplaytutorial
 
Unity - Building Your First Real-Time 3D Project - All Slides
Unity - Building Your First Real-Time 3D Project - All SlidesUnity - Building Your First Real-Time 3D Project - All Slides
Unity - Building Your First Real-Time 3D Project - All Slides
 
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
Introduction to Game Programming: Using C# and Unity 3D - Chapter 2 (Preview)
 
Ninja Cat Flyer - 1 of 6
Ninja Cat Flyer - 1 of 6Ninja Cat Flyer - 1 of 6
Ninja Cat Flyer - 1 of 6
 

Recently uploaded

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 
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
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
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
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
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
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

Lecture2_practice.pdf

  • 2. Game Development • Sprite: is an image that can be used as a 2D object. Sprites, by default, are rendered directly against the camera. • Textures: graphical components. These are also images, but they are used to change the appearance of an object in both 2D and 3D. But Textures are not objects. This means that you cannot get them to move during gameplay. • Rigidbody: A Rigidbody 2D component places an object under the control of the physics engine. • There are three options for Body Type which defines the movement (position & rotation) behavior collider interaction: • Dynamic • Kinematic • Static
  • 3. • Collider: Collider components define the shape of a GameObject for the purposes of physical collisions. A collider, which is invisible, does not need to be the exact same shape as the GameObject. • The scripting system can detect when collisions occur and initiate actions • OnCollisionEnter2D: When the collision is detected. • OnCollisionStay2D: During updates where contact is maintained, • OnCollisionExit2D indicates that contact has been broken
  • 4. Simple Project • Open unity and select new project • Select 2D from the template and change the project name from the project setting
  • 5. Simple Project • Create Sprite (2 of them). I will choose square. Called the first one Player and the second one Ground.
  • 6. Simple Project • Change the size of the ground to be rectangle to look like a ground. You can do that by clicking on the gameObject on the Hierarchy view and and select the Rect Tool from the top bar. Then you can see blue dot on the corner of the object to change the size.
  • 7. Simple Project • Add Box Collider 2D to the ground. Click on the Ground object and from the Inspector view click on the add Component and select “Box Collider 2D”. • Select Player object and add “Rigidbody2D” and Capsule Collider 2D from add component on the Inspector view.
  • 8. Simple Project • Create folder of the C# scripts. Click on the Assets from the Project View, right click on the Assets view and select Create and then Folder, name the folder as Scripts. • Go inside the Scripts folder (double click) and create script file (right click -> Create -> C# Script) and name it “PlayerController”.
  • 9. Simple Project • After that drag the C# file over the Player gameObject (adding the script to the Player GameObject). Now if you click on the Player, you can see the script in the inspector view. To edit the code double click on the C# file which will open it with Visual Studio (VS). To make sure that the code open with VS, go to Edit -> Preferances -> External Tools and change the “External Tool Editor” to VS
  • 10. Simple Project • After adding the C# code, the object will keep jumping. To solve this issue, we can add a collider (Box Collider 2D) to the Player objects lightly below the object to help deciding when to jump. Then we need to check the isTriger, this will just triger an action without the actuality of player hitting something.
  • 11. Simple Project • To avoid the rotation of the Player when moving side, on the Rigidbody2D check option on the Constraints - >Freeze Rotation. • Now we need to change the Tag for the objects. Set the Player object with tag “Player” and create new tag “Platform” for the Ground. Click on the Player object and from the inspector change the tag. To create one, choose Add Tag and click the + sign and enter the name of the new tag and save. Go back to Ground object and choose the new created tag
  • 12. Simple Project • To control how the player move we can change the Gravity and Linear drag from the Rigidbody2D. Also setting Collision detection to continuous with prevent the object from going through another object when there is a collision.
  • 13. The C# Script • Define class variable private Rigidbody2D rigidbody2DObject; private float moveSpeed; private float jumpForce; private float moveHorizantal; private float moveVertical; private bool isJumping;
  • 14. In the start function we need to get the rigid body component and set the values void Start() { rigidbody2DObject = gameObject.GetComponent<Rigidbody2D>(); moveSpeed = 2f; jumpForce = 60f; isJumping = false; }
  • 15. In the update function we get the values for the horizontal (left/right) and vertical. Then based on these value the object move. void Update() { moveHorizantal = Input.GetAxisRaw("Horizontal"); moveVertical = Input.GetAxisRaw("Vertical"); if(moveHorizantal > 0 || moveHorizantal < 0) { // move the player left or right rigidbody2DObject.AddForce(new Vector2(moveHorizantal * moveSpeed, 0), ForceMode2D.Impulse); } if(!isJumping && moveVertical > 0f) { // move the object up rigidbody2DObject.AddForce(new Vector2(0f, moveVertical*jumpForce),ForceMode2D.Impulse); } }
  • 16. • To detect when to stop jumping we call the collision functions. void OnTriggerEnter2D(Collider2D collision) { // when the collsion start disable the ability to jump if(collision.gameObject.tag == "Platform") { isJumping = false; } } void OnTriggerExit2D(Collider2D collision) { // when the player return to the ground enable the player to jump if (collision.gameObject.tag == "Platform") { isJumping = true; } }