The Box Collider 2D component will give our character a bounding box that will interact with the world around it. This means that if the character hits another GameObject that also has Collider, we will be able to use code to know about it. This is imperative to the interaction of the character with the game world since without it, we wouldn't have the information needed to do things, such as collect pick-ups, or know when the character hit something dangerous, such as the obstacles.
Just as Rigidbody 2D, the Box Collider 2D component is easy to set up.
By default, Box Collider 2D will fit itself around the image. As our image is 32 x 32 pixels wide and high by default, the collider will match it as it is also 32 x 32 pixels wide and high. To make it fit our character better, change the X value of Size down to 0.22
:
This area is the basis of our character. It will simulate gravity, collide with anything else that is representative of a collider, and take in any sort of force we want to give it, such as changing its velocity when the player swipes on the mobile device.
In order to test our changes, we will need to create a temporary floor that will prevent the character from falling forever.
50
.We will also need to position the character in the world correctly. To perform this, start by selecting the Character GameObject.
0
on X, 0
on Y, and 0
on Z. This will put the character in the very center of our game world.0
on X, -1
on Y, and 0
on Z. This will put a collider just after Character, preventing it from falling.1
. If Camera Projectile isn't set to Orthographic, make sure it is set to this.If you play the game now, you will see the character fall a little and then stop and run in place. If you don't see this, make sure to revisit the preceding steps.