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.
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;
}
}