Javatpoint Logo
Javatpoint Logo

Understanding Collisions

As you know, everything in your game is a GameObject. Even the individual tiles that make up your level are GameObjects by themselves.

When we consider every component as a GameObject, we understand that there could be thousands of GameObjects in a scene, interacting with each other in some way. You can visualize that if unity added collisions to every single GameObject, it would be impractical for the engine to calculate collisions for every single one of them.

In Unity, collisions are separated from the actual sprite itself, attached as separate components, and are calculated on their own.

Collider components describe the shape of an object for the purposes of physical collisions. A collider will be invisible and is required to be the exact same shape as the GameObject's mesh.

Let's add a simple wall that our player can collide against. First of all, create a sprite. To do that, right click on your scene from the Hierarchy tab and select 2D object -> Sprite.

Understanding Collisions

Rename the New Sprite to Player.

Or directly go to the project tab and right click on Assets and select Create -> Sprites -> Square.

Understanding Collisions

Drag that square to the scene.

Understanding Collisions

Now go to Add Component in the Inspector tab, and search for "Box Collider 2D".

Understanding Collisions

You will see a bright green line on the perimeter of your Player GameObject. This is the collision boundary. It defines the actual shape of the collidable object.

Understanding Collisions

We can create collisions of different shapes in different sizes. They can be in a rectangular shape, or even they can be in polygonal shapes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA