Professional Documents
Culture Documents
Unity 2D Lecture
Unity 2D Lecture
Unity 2D Lecture
UNITY - 2D
Add Sprites, move objects, collecting things, and UI interaction
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Instantiation
ADDING ASSETS
- Drag the le you want to add to your game, and drop it into the resource manager
section.
- If the sprite contains animated image (series of images) we will use the slice tool to
create the animation (will be explained in last topic)
fi
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Instantiation
SPRITES
- The sprite is a component makes game object visible to the camera
- Drag any image from the assets to the scene it will create a game object that contains
the sprite renderer component
- You can create empty object and add the sprite renderer component and this will create
a sprite object as well.
ff
SPRITES
- In this component we add the image in the
sprite variable.
ff
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Instantiation
RIGIDBODY
- The sprite renderer will only make it visible, its just an image, it can not a ect or be
a ected by other game objects
- Rigidbody will make it alive, and give it the physical characteristics such as gravity
- Once you add the Rigidbody2D to the game object it will fall down due to the gravity
e ect.
ff
ff
ff
RIGIDBODY
- The variables in rigidbody a ect the
player physics
ff
ff
ff
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Instantiation
COLLIDERS
- Collider component is the component that is responsible of de ning the collision area
and detect collisions with other objects
- Without collider objects move through each other without knowing that they are
passing through each others.
OBJ 1
OBJ 2
- Without colliders the objects will be like
this
fi
COLLIDERS COLLISION - THEY WILL NOT PASS +
fi
COLLIDERS
- There are several types of colliders based on the sprite shape
- Box Collider 2D
- Circle Collider 2D
- Capsule Collider 2D
- Polygon Collider 2D
- Don’t use the polygon collider too much since it is a heavy process to detect the
edges of the sprite and using it too much will increase the processes of CPU at
runtime.
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Instantiation
COLLISIONS
- When tow objects collides and they are both contains colliders (not trigger) each of
these objects will call one of the following functions based on the collision status
OBJ 1
OBJ 2
OBJ 1
OBJ 2
- if this code is written in Obj 1 => then will points to the collider on Obj 2
- The contact points of the collision

fi
COLLISIONS
Normal = ( 0 , 1 ) Normal = ( 0 , -1 )
Normal = ( 1 , 0 ) Normal = ( -1 , 0 )
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Instantiation
TIGGERS
- Trigger (is trigger) is an attribute in the collider component means that this is not a solid
object and things can pass through it but it will trigger a function if any object passes
through it
- When two game objects have colliders and they collides the OnCollisionEnter function
is called.
- But if one of them contains a trigger collider, they both will can OnTriggerEnter function.
1 2 3 4
TRIGGER
ON TRIGGER ENTER
ON TRIGGER EXIT
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Winning and Losing in a game.
- Instantiation
INSTANTIATION
- Instantiation means to create an object in the scene while the game is running.
- to create the game object in the run time it should be prepared and stored in the assets
INSTANTIATION
- Double click on the prefab, then start add the component you need to the prefab.
- When you want to create the prefab use the instantiation function
INSTANTIATION
1
3
INSTANTIATION
f a b to th e v ariable
Drag the pre
INSTANTIATION
f a b to th e v ariable
Drag the pre
INSTANTIATION
- Another way to reach out the prefabs without de ning a serialize variable is to create a
resources folder and reach the prefabs directly from there.
fi
INSTANTIATION
YOU CAN PUT YOUR PREFABS DIRECTLY IN THE RESOURCES FOLDER,
OR YOU CAN MAKE IT MORE ORGANIZED AND CREATE A FOLDER
CALLED PREFABS INSIDE THE RESOURCES, AND PUT YOUR PREFABS
INSIDE IT.
INSTANTIATION
📁 Assets
GameObject myPrefab = Resources.Load<GameObject>(“Example Prefab”);
📁 Assets
INDEX
- Adding Assets to 2D Game
- Sprites
- Rigidbody
- Colliders
- OnCollisionEnter - Stay - Exit
- OnTriggerEnter - Stay - Exit
- Winning and Losing in a game.
- Instantiation
- Animation
ANIMATION
- Animation in unity uses the idea of timelines
- Two main les we use to perform animation in unity
- Animation le (Animation Clip): which is the timeline that do the changes on some
property.
- Animator le: which is the le that changes between animation clips.
fi
fi
fi
fi