Adding Box Collider 2D

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.

  1. With the Character GameObject selected, navigate to the Inspector tab and click on Add Component.
  2. Search for Box Collider 2D and select Box Collider 2D.

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:

Adding Box Collider 2D

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.

  1. In Hierarchy, right-click and select Create Empty.
  2. Select the new empty GameObject and in Inspector, click on Add Component.
  3. Search for Box Collider 2D and select Box Collider 2D.
  4. With GameObject still selected, change the X value of Size as 50.

We will also need to position the character in the world correctly. To perform this, start by selecting the Character GameObject.

  1. In Inspector, change its Position in the Transform component to 0 on X, 0 on Y, and 0 on Z. This will put the character in the very center of our game world.
  2. Next, select the empty GameObject that is representative of the temporary floor.
  3. In its Transform component, select the Position as 0 on X, -1 on Y, and 0 on Z. This will put a collider just after Character, preventing it from falling.
  4. To have a better view of Character, select the Main Camera GameObject in the Hierarchy tab. In Inspector, select the Size as 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.

  • If the character isn't falling, make sure that you have Rigidbody 2D on it.
  • If the character isn't playing our running animation or is playing the wrong animation, make sure to check whether the controller is correctly assigned and set up.
  • If the character doesn't have a sprite, view Sprite Renderer to make sure that a sprite is assigned.
  • If the character is falling forever, make sure to navigate to the temporary floor we made and Box Collider 2D to verify that it is present and sized correctly. If the floor is set correctly, make sure Character has a Box Collider 2D component on it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset