Unity 2D Lesson
TABLE CONTENT
COMPONENT..................................................................................................1
1. Transform...............................................................................................1
2. Rigidbody 2D..........................................................................................5
COMPONENT
1. Transform
The Transform stores
a GameObject’s Position, Rotation, Scale and parenting state. A
GameObject always has a Transform component attached: you can’t
remove a Transform or create a GameObject without a Transform
component.
The Transform component determines the Position, Rotation,
and Scale of each GameObject in the scene. Every GameObject has a
Transform.
Properties
Proper
Function:
ty:
Positio Position of the Transform in the x, y,
n and z coordinates.
Rotatio Rotation of the Transform around the x-
n axis, y-axis, and z-axis, measured in
degrees.
Scale Scale of the Transform along the x-
axis, y-axis, and z-axis. The value “1”
is the original size (the size at which
you imported the GameObject).
Note: Unity measures the Position, Rotation and Scale values of a
Transform relative to the Transform’s parent. If the Transform has no
parent, Unity measures the properties in world space.
Editing Transforms
In 2D space, you can manipulate Transforms on the x-axis or the y-axis
only. In 3D space, you can manipulate Transforms on the x-axis, y-axis,
and z-axis. In Unity, these axes are represented by the colors red,
green, and blue respectively.
There are three primary ways you can edit a Transform’s
properties:
In the Scene view
In the Inspector window.
In your C# scripts.
Scene view
In the Scene view, you can use the Move, Rotate and Scale tools to
modify Transforms. These tools are located in the upper left-hand
corner of the Unity Editor.
You can use the Transform tools on any GameObject in a scene. When
you select a GameObject, the tool Gizmo appears within it. The
appearance of the Gizmo depends on which tool you select.
Img: The transform Gizmo with keyboard shortcuts in brackets.
When you click and drag on one of the three Gizmo axes, the axis’s
color changes to yellow. While you drag the mouse, the GameObject
moves, rotates, or scales along the selected axis. When you release the
mouse button, the axis remains selected
While moving the GameObject, you can lock movement to a particular
plane (that is, change two of the axes and keep the third unchanged).
To activate the lock for each plane, select the three small coloured
squares around the center of the Move Gizmo. The colors correspond to
the axis that locks when you select the square (for example, select the
blue square to lock the z-axis).
Inspector window
In the Inspector window, you can use the Transform component to edit
the Transform properties of a selected GameObject. There are two ways
to edit the Transform property values in the component:
Enter values into the property value fields manually. This is useful
for very specific adjustments.
Click a value field and drag up or down to increase or decrease
the value. This is useful for less specific adjustments.
From code
Use the Transform class in your C# code to modify a GameObject’s
position, rotation, and scale, as well as its hierarchical relationship to
parent and child GameObjects.
For a complete reference of every member of the Transform class and
usage examples, refer to the Transform API reference.
Grouping GameObjects
In Unity, you can group GameObjects into parent-child hierarchies:
A parent GameObject has other GameObjects connected to it that
take on its Transform properties.
A child GameObject is connected to another GameObject, and
takes on that GameObject’s Transform properties.
In the Hierarchy window, child GameObjects appear directly
underneath parent GameObjects and are indented in the list. You can
select the fold-out icon to hide or reveal a parent GameObject’s child
GameObjects.
A child GameObject moves, rotates, and scales exactly as its parent
does. Child GameObjects can also have child GameObjects of their
own. A GameObject can have multiple child GameObjects, but only one
parent GameObject.
These multiple levels of parent-child relationships between
GameObjects form a Transform hierarchy. The GameObject at the top of
a hierarchy (that is, the only GameObject in the hierarchy that doesn’t
have a parent) is known as the root GameObject.
To create a parent GameObject, drag any GameObject in the Hierarchy
window onto another. This creates a parent-child relationship between
the two GameObjects.
Editing Transforms for parent and child GameObjects
The Transform values for any child GameObject are displayed relative
to the parent GameObject’s Transform values. These values are called
local coordinates. For scene construction, it is usually sufficient to work
with local coordinates for child GameObjects. In gameplay, it is often
useful to find their global coordinates or their exact position in world
space.
Tip: When you parent Transforms, it is useful to set the parent’s
location to <0,0,0> before you add the child Transform. This means
that the local coordinates for the child Transform will be the same as
the global coordinates, which makes it easier to ensure the child
Transform is in the right position.
Note: Never set any Scale axis to 0 in the Transform component. Doing
so can cause undefined results for math calculations, such as NaN (Not
a Number) values in rendering, which leads to incorrect graphical
artifacts on screen.
Changing the Scale affects the position of child Transforms. For
example, scaling the parent Transform to (0,0,0) positions all child
Transforms at (0,0,0) relative to the parent Transform.
2. Rigidbody 2D
What is Rigidbody 2D:
- Rigidbody 2D is a component attached to GameObjects to allow
them to interact with Unity's 2D physics system. It enables
objects to respond to forces, gravity, and collisions according to
real-world physical rules (such as mass, friction, and inertia
How a Rigidbody 2D works
- The Rigidbody 2D component overrides the Transform
component and updates it to the position and/or rotation it
defines instead.
Note: You can override the Rigidbody 2D by directly modifying the
Transform component yourself (because Unity exposes all properties on
all components). However, this will cause issues such as unpredictable
movement or GameObjects passing through and into each other.
Collider 2D and Rigidbody 2D interaction
- Any Collider 2D component added to the same GameObject or
child GameObject is implicitly attached to that Rigidbody 2D
GameObject, causing the Collider 2D to move with the Rigidbody
2D.
- When attached, you should never move the Collider 2D directly
using the Transform or any collider offset; move the Rigidbody 2D
instead. Moving the Rigidbody 2D provides the best performance
and ensures correct collision detection.
- Collider 2Ds attached to the same Rigidbody 2D won’t collide with
each other.
Note: Although Rigidbody 2Ds are often described as colliding with
each other, it’s the Collider 2Ds attached to each of those bodies which
collide. Rigidbody 2Ds can’t collide with each other without Colliders.
The Attributes of Rigidbody 2D
Body Types: The selected Body Type defines the Rigidbody 2D’s
movement behavior (position and rotation) and Collider interactions.