C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Understanding CollisionsAs 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. Rename the New Sprite to Player. Or directly go to the project tab and right click on Assets and select Create -> Sprites -> Square. Drag that square to the scene. Now go to Add Component in the Inspector tab, and search for "Box Collider 2D". 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. 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.
Next TopicUnderstanding Prefabs & Instantiation
|