Unity 3D - Handling Collisions of GameObject using Collider2D
Collisions are Unity's way of understanding the physical interaction between two Rigidbodies, and indeed, two gameObjects in general. In real life, when we say two objects collided, we mean that they touched each other physically.
In Unity, collisions aren't defined by Rigidbodies themselves, but instead by a new component called Collider2D. Collider2Ds are components which define a region in which collision interaction between gameObjects occur. In simple terms, colliders are simply the defined regions where gameObjects are solid to other gameObjects. We stress on the fact that they're regions because not all colliders are shaped exactly like the gameObject that defines them. In fact, for highly detailed models and sprites, most developers use basic shapes that make an approximate shape.
In the above images, note the green boundaries. Those boundaries are what define the collision region in the respective gameObjects. In the triangle, the boundary fits the shape of the gameObject perfectly since we used a simple triangle. In the square, we made the collision region larger than the actual shape of the gameObject to once again make the point, that the Collider is an independent region from the gameObject.
The most interesting case is the Gear object. Note how the collider doesn't wrap around the gear's teeth perfectly, but instead Unity made approximations on the collision regions of the shape, leading to a simpler collision region. (We didn't make that collision region, Unity auto-generated it. We will show you how to do that yourself very soon).
So, let's put this knowledge of colliders to use. We don't even have to use scripting, yay! In your project, open up your main character, and add a Box Collider 2D
by going to Add Component → Physics 2D → Box Collider. You'll now notice that a new component, the Box Collider, has showed up in the Inspector pane.
Of course, the size value may vary depending on how your image is shaped.
You see the Edit Collider option that we have highlighted in the above image. If you click on that button, you'll notice that your image now has a few handles which you can use to adjust the size of the collider region.
If you run the game right now, you won't really notice any difference. Your character will still move like normal using the arrow keys. So, let's test out the real magic of colliders by giving our main character something to collide with. To do so, right click in the Assets area and go to Create → Sprites.
This menu gives you a lot of basic shapes which you can use as placeholder sprites or for testing out interactions. Click on Square and it will generate a new Square shape for you to use.
Press Enter, and then drag and drop the Square into the Hierarchy, just like we added Mr. Star.
Use the blue handles to resize the square to a decent size, so that you can test out the collisions. Something like the following image should suffice.
If you run the game now, you'll see that Mr. Star doesn't really care for the obstacle in his way, and he'll simply slide smoothly under the white block.
How do we fix that? Here's how: Without changing anything else, add a Box Collider 2D
to the new Square sprite as well. Since it's a basic rectangular shaped object, Unity will automatically add a Collider region exactly like the sprite shape. Now again, try and run the game.
Our character now collides with the white block like we want it to! But as soon as you try to turn the character around the block's corner while pushing against it, things start going a little wonky.
Well, let's have a look at the whole scenario from Unity's point of view. While playing the game, simply click on the Scene tab to see what's going on behind the scene. If you click on your main character, you might see something like this:
There's the problem! You tried to push the square collider against a corner, thus rotating it. In fact, here's a way you can test out this in real life, without even getting up from your computer seat. Try pushing the monitor (gently!) in the middle. It'll shift back a bit, right? But try pushing it against a corner. What happens? Your monitor rotates. If you've studied a bit of advanced physics, you'll know that what you're doing is applying a torque to your monitor, making it turn.
Back to the game, how do we fix our character from going wonky around a corner? It's simple. Simply open up your main character, and in the Rigidbody2D properties, open up the Constraints section.
If you open that menu (by clicking the triangle), you'll see options to disable (or freeze, as Unity calls it) the object's movement and rotation. Just tick the Freeze Rotation Z box, and you're good to go. But it still isn't quite satisfactory. There's a big spacial gap between the two colliders where you can't move because of the box shape of the colliding regions.
How do we make the collider a bit more realistic in shape? Just like our star. Well, instead of a Box Collider, let's move on to the slightly more complex version of it, the Polygon Collider
. To add a Polygon Collider 2D, let's first get rid of the box collider that's already attahced to our character. Simply click on the gears (to open settings) on the top right of the component (We have highlighted it in the picture), and select the option, Remove Component.
Now follow the same steps that we followed while adding a Box Collider, but this time, select a Polygon Collider instead. If your character has a fairly simple shape, Unity shouldn't have much problems in defining a similarly shaped collision region automatically for you. If it doesn't, however, as was the case for Mr. Star, we will have to do it on our own.
Simply click on the Edit Collider button to edit the collider's boundary region.
After a bit of editing on Mr. Star's collider, this is the final product we came up with:
Try testing out your new collider in the game.
Now, you may want to save your work. You can simply save it by pressing Ctrl + S
, and saving it as a scene. We have explained how scenes work in previous lessons, and further down the road, we will also teach you how to work with multiple scenes.
Practice Exercise
-
Try making the player bound inside the game, so he doesn't wander off-screen. Put colliders to use by making new sprites.
HINT: The main camera is standing still, so you have a fairly good idea of where to put the colliders by judging against the edges of the camera.
- Once you get the hang of that, make your first actually playable game! Make a maze out of colliders and sprites, and try to have a main character find his way out! You can even try out importing images with Start and Finish text in them and placing them in the game to help the player.