Chapter 2 - Unity 2d Basics
Chapter 2 - Unity 2d Basics
Unity 2D Basics
GAME PROGRAMMING – TRINH THI NGOC LINH
1
Creating First Project
Open Unity
Click Projects -> New Project button
2
Creating First Project
Select the 2D template
Set Project Name and Location
Click the Create project button
3
Creating First Project
4
Opening a Project
Click Project name
Or click Open button
5
Scenes
Scenes are where you work with content in Unity
They are assets that contain all or part of a game or application
You can create any number of scenes in a project
For example:
You might build a simple game in a single scene
With a more complex game, you might use one scene per level, each
with its own environments, characters, obstacles, decorations, and UI
6
Scenes
• Creating a new scene:
• Creating a new scene from the menu:
• Use the menu: Assets -> Create -> Scene
• Creating a new scene from the project window:
• Navigate to the folder where you want to create the new scene
• Right click the folder, or right-click an empty area in the right hand
pane, and select Create -> Scene from the context menu
7
Scenes
8
Camera component
• The devices that capture and display the world to the player
• By customizing and manipulating cameras, you can make the
presentation of your game truly unique
• You can have an unlimited number of cameras in a scene
9
Adding an asset from the Unity Asset Store to project
• In top menu: Select
Window -> Asset Store
• Click Search online, go to
website:
https://assetstore.unity.com/
• Log in to your Unity account
• Search for assets
• Click Buy Now / Add to My
Assets button
10
Adding an asset from the Unity Asset Store to project
• Click Open Package
Manager
• Select Package from
My Assets
• Click Download
11
Adding an asset from the Unity Asset Store to project
• Click Import to import
assets from My assets to
project
12
GameObject
• GameObject is a fundamental building block and a core concept
representing any object that can be placed in a scene
• It is the base class for all entities that can exist in your game world,
including:
• Characters
• Items
• Obstacles
• Lights
• Cameras
• ...
13
GameObject
• Hierarchy Structure:
• GameObjects follow a hierarchical
structure, forming a tree-like
representation called the Hierarchy
• Each GameObject can have zero or more
child GameObjects, and each child can
have its own children, creating a parent-
child relationship
• This hierarchy allows you to organize your
scene and define how objects are
positioned relative to each other
14
GameObject
• A GameObject itself doesn't have
any visible or interactive properties
on its own. It is essentially an
empty container
• GameObjects serve as containers
for components that define their
functionality, appearance, and
behavior
15
Component
• Components are the functional pieces of every GameObject
• Components contain properties which you can edit to define the
behavior of a GameObject
16
Component
• Some common components include:
• Transform (position, rotation, and scale)
• Rigidbody (physics properties)
• Mesh Renderer (visual appearance)
• Collider (collision boundaries)
• Scripts
• Audio Source
• Particle System (Generates various visual
effects like smoke, fire, or explosions)
17
Sprite
• Sprites are simple 2D objects
that have graphical images
(called textures) on them
• Unity uses sprites by default
when the engine is in 2D
mode. When viewed in 3D
space, sprites will appear to
be paper-thin, because they
have no Z-width
18
Sprite
• Creating a sprite
• By dragging from an
external source into
Unity, we are adding
an Asset. This Asset is an
image, so it becomes
a texture
• Dragging this texture
into the scene hierarchy
19
Transforms and Object Parenting
• Transforms have three visible properties:
• The position
• The rotation
• The scale
• Each of properties have three values for the three axes. 2D games
usually do not focus on the Z-axis when it comes to positioning
• The rotation properties define the amount of rotation (in degrees)
an object is rotated about that axis with respect to the game
world or the parent object
20
Transforms and Object Parenting
• The scale of an object defines
how large it is when compared
to its original or native size
• For example, let us take a
square of dimensions 2x2. If
this square is scaled against
X-axis: 3 -> 2 x3 = 6
the X-axis by 3 and the Y-axis Y-axis: 2 -> 2 x 2 = 4
by 2, we will have a square of
size 6x4
21
Transforms and Object Parenting
• GameObjects can become parents of other GameObjects
• When a GameObject has a parent, it will perform all its transform
changes with respect to another GameObject instead of the game
world
22
Transforms and Object Parenting
• For example:
• An object with no parent placed at (10,
0, and 0) will be at a distance of 10 units
from the game world’s centre
• However, a gameObject with a parent
placed at (10, 0, 0) will consider
the parent’s current position to be the
centre
23
Tag and Layer
• Tag
• A tag is a label or identifier that you can
assign to GameObjects to categorize and
differentiate them based on their
characteristics or purposes
• Tags are commonly used to group objects
together and provide an easy way to
reference and interact with them in scripts
and components
• For example:
• Tag: "Player"
• Tag: "Enemy" 24
Tag and Layer
• Applying Tag:
• Select the GameObject
• In the Inspector window, look for the "Tag" field
• Click on the drop-down arrow next to the "Tag"
field to see the list of existing Tags
• To apply an existing Tag to the GameObject, click
on the desired Tag from the list
• If you want to create a new Tag, scroll to the
bottom of the list and click on Add Tag.... A new
window will appear, allowing you to create a
new Tag. Enter the name for your new Tag and
click Save 25
Tag and Layer
• Layer
• A layer is a way to categorize and
separate GameObjects based on their
interaction and rendering properties
• Layers are particularly important for
controlling how objects interact with
each other in terms of physics and
collision detection, as well as how they
are rendered and affected by lighting
26
Tag and Layer
• Layer
• For example: In a top-down shooter game, you may want to create a
layer for enemies to control how they interact with the player and other
objects:
• Layer: Player
• Layer: Enemy
• Layer: Obstacle
• For example: In an open-world game, you might have multiple cameras
showing different areas of the game world. To optimize performance,
you can control which objects are visible to each camera using layers:
• Layer: Player
• Layer: Environment
• Layer: UI
27
Tag and Layer
• Applying Layers:
• Select the GameObject
• In the Inspector window, look for the "Layer" field
• Click on the drop-down arrow next to the "Layer"
field to see the list of existing Layers
• To apply an existing Layer to the GameObject,
click on the desired Layer from the list
• If you want to create a new Layer, scroll to the
bottom of the list and click on Add Layer.... A new
window will appear, allowing you to create a new
Layer. Enter the name for your new Layer and
click Save
28
Script
• Script refers to a piece of code
written in a programming
language like C# (or
occasionally Unity's built-in
JavaScript, though it's less
commonly used)
• Scripts are used to define the
behaviors and interactions of
GameObjects in your game
29
Collider
• Colliders are components
which provide collision
detection through the
various green lines (or
boundaries)
• They define the shape of
game objects for the
purposes of physical
collision
30
Collider
• Some of the common types of colliders:
Tilemap Collider 31
Collider
• Applying a collider:
• Select the GameObject
• Add a Collider Component: With the GameObject selected, go to the
Inspector window -> click on the Add Component button
• Select the Collider Type
• Configure the Collider: Once the Collider component is added, you can
adjust its properties in the Inspector. The specific properties you can
configure depend on the type of Collider you selected. For instance, a
Box Collider allows you to set the size and center of the box, while a
Sphere Collider lets you adjust the radius, and so on
32
Collider
34
Rigidbody
• Applying a Rigidbody:
• Select the GameObject
• Add a Rigidbody Component:
With the GameObject selected,
go to the Inspector window ->
click on the Add Component
button
• Search for Rigidbody: In the
search bar that appears, type
"Rigidbody“ and select the
Rigidbody Component
35
Rigidbody
• Configure the Rigidbody
• Mass: Sets the mass of the Rigidbody. Higher mass makes the object
harder to move or affect by external forces
• Linear Drag: Applies a damping force to slow down the Rigidbody's linear
motion. Higher drag values result in slower movement
• Angular Drag: Applies a damping force to slow down the Rigidbody's
angular rotation. Higher angular drag values result in slower rotation
• Gravity scale: Determines how much gravitational force affects the object
• Constraints: You can limit the movement of the Rigidbody along certain
axes by checking the corresponding checkboxes
36
Prefab
• Prefab (short for "prefabricated") is
a special type of asset used to
store and manage reusable
GameObjects with components
and settings
• Prefabs act as templates for
creating instances of GameObjects
in your scenes. They allow you to
define a set of properties,
components, and child
GameObjects that you can reuse
throughout your game project
37
Prefab
• To create a Prefab:
• Create a GameObject in the Unity scene
• Configure the GameObject
• Drag the GameObject from the Hierarchy window into the Project
window. This creates a Prefab asset with the same configuration as the
GameObject.
• To use a Prefab:
• Drag the Prefab from the Project window into the scene to create an
instance of that Prefab
• You can now modify the instance as needed. Any changes you make will
be unique to that instance and will not affect other instances or the
original Prefab in the Project window
38
Animation
• Animation refers to the process of creating motion and changes in
the state of objects within a game
• It allows you to bring flexibility to characters, objects, and effects
by adding movement, transformation, and visual changes.
39
Animation
• Animation Clip:
• An Animation Clip defines a specific set of keyframes
• Each Animation Clip contains information about all the keyframes and
value changes over a specific time period
• Animator Controller:
• This is a crucial component for controlling the animations of an object
• The Animator Controller consists of states and transitions between states
40