Unity 2D Lecture

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

AHMED QAZZAZ - 2022

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.

- Images in 2D games mode, will be considered to be Sprites(2D and UI) automatically,


no need to change the texture type as we did in the UI example.

- 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

- The sprite is an image

- It is di erent from Image game object

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

- Other variables will a ect the appearance of


the sprite in the scene.

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

- for example Gravity Scale will a ect the


power of the gravity e ect on the
object.
- If it higher number the player may
not be able to jump.
- if it smaller number the player may
fell like being in the moon.

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 +

- Adding the collider object will make


the objects colliders and stop (i.e. it
FUNCTION WILL BE CALLED

will prevent objects from passing OBJ 1


through each others).
OBJ 2

TRIGGER - THEY WILL PASS +

- Colliders can also be identi ed as


Triggers which means they objects
FUNCTION WILL BE CALLED

will pass through each other but the OBJ 1


collision will also be detected.
OBJ 2

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

FRAME Object OnCollisionEnter2D OnCollisionStay2D OnCollisionExit2D


# obj 1 ✅
1
obj 2 ✅
obj 1 ✅
2
obj 2 ✅
obj 1 ✅
3
obj 2 ✅
obj 1 ✅
4
obj 2 ✅
COLLISIONS
- now the objects stops the collision and move away (e.g. at frame 7)

OBJ 1

OBJ 2

FRAME Object OnCollisionEnter2D OnCollisionStay2D OnCollisionExit2D


# obj 1 ✅
5
obj 2 ✅
obj 1 ✅
6
obj 2 ✅
obj 1 ✅
7
obj 2 ✅
obj 1
8
obj 2
COLLISIONS
- Each of the three function will pass a Collision2D object to the function.

- it contains info about the collision that happens

- The collider of the object


- The other collider

- if this code is written in Obj 1 => then will points to the collider on Obj 2
- The contact points of the collision

- will get the rst contact collision point


- .normal => Surface normal at the contact point.
- That is the vector that is perpendicular to the surface


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.

TIGGERS COLLIDER THAT IS NO TRIGGER


COLLIDER THAT IS TRIGGER

1 2 3 4
TRIGGER

ON TRIGGER ENTER

ON TRIGGER STAY ON TRIGGER STAY ON TRIGGER STAY

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

- The game object that is stored in the assets is called Prefab

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

VARIABLE THAT REFERRING TO ROTATION TRANSFORMATION


THE PREFAB MATRIX

THE POSITION THAT YOU WILL USE TO


CREATE THE PREFAB

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.

CREATE A FOLDER IN THE ASSETS ROOT, AND CALL IT


RESOURCES, IT SHOULD BE CASE SENSITIVE AND MATCH THE
SAME NAME AS THE EXAMPLE

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”);

Instantiate(myPrefab, transform.position, Quaternion.identity);


📁 Resources
🗳 Example Prefab

📁 Assets

GameObject myPrefab = Resources.Load<GameObject>(“prefabs/something”);


📁 Resources
Instantiate(myPrefab, transform.position, Quaternion.identity);
📁 prefabs
🗳 something

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.

Animation Clip Animator


fi
fi
fi
fi

You might also like