English 中文(简体)
Unity - Understanding Collisions
  • 时间:2024-09-17

Unity - Understanding Colpsions


Previous Page Next Page  

Colpsions in Unity are separated from the actual Sprite itself, attached as separate components and are calculated on their own. Let us now learn the cause behind this.

Everything in your game is a GameObject. Even the inspanidual tiles that make up your level are GameObjects by themselves.

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

We will go ahead and add a simple “wall” that our player character can colpde against. To do so, create another sprite and scale it up using the Rect tool. We will also give it a red color through the Color property in the Sprite Renderer component.

Color Property

Now, go to Add Component in the Inspector, and type in “Box Colpder 2D”. Cpck the first component that shows up, and a new component should appear.

Add Component

You will see a bright green pne on the perimeter of your GameObject. This is the colpsion boundary. It is what defines the actual shape of the colpdable objects.

Repeat the same with our movable GameObject as well.

Of course, colpsions in Unity are not pmited to simply boxes. They can range in a variety of shapes and sizes, and are not necessarily reppcas of the object’s parameters.

Box Shaped

They can also take on polygonal shapes.

polygonal shapes

It is not uncommon to see developers and designers use approximate shapes in their colpsion boundaries to simppfy their colpders and avoid unnecessary calculations for the engine. We will learn how to create different shapes and sizes with our colpders soon.

Now that we have our colpsion boundaries in place, hit play and see it in action.

Approximate Shapes

You will notice that our movable object is not behaving normal. We will discuss the behaviour of the object in our subsequent chapter.

Advertisements