Adding Assets to Your Game

No more blocks attacking blocks!!

Objective: Add some real UI assets to upgrade our blocks

Adding a Background and adjusting Sorting Layers

Unity makes it very simple to create a GameObject out of a sprite. Simply drag the sprite onto the hierarchy and Unity will create a GameObject for you.

Once the object is created you can rename is Background and resize it to fill the screen.

The last step in our background setup is to set up a Sorting Layer. A sorting layer sets up the images in your scene in a set of layers. As you can see in the demo below, the background in this case is above the ship. This may be wanted behavior in some games but not this one. We are not hiding in nebulae.

Layers prevent the Player from being hidden by the background

For our setup we will have the background in the Background layer and all other assets in the Foreground layer. The layers are drawn in the order they are listed so Default, then Background, and finally Foreground.

Player, Enemy, and Laser

Before removing the old PreFab object copy out the settings you have selected for the script inputs. Then paste the values to the new GameObject script.

Since we are switching form 3D to 2D we need to add a Collider2D. For our case we will use a Box Collider 2D. Make sure that it is also set as trigger so no physics collisions occur.

Adjust the size of the box collider so that it is close to the size of the sprite.

Similar to Player and Laser, Enemy will require similar changes plus one extra. It will need a RigidBody2D component. With gravity scale set to 0.

Updating our script and tags

Change the method name from OnTriggerEnter to OnTriggerEnter2D and the parameter type from Collider to Collider2D.

Everything should be good now but we forgot to change one thing for Player and Laser on re-creating our GameObjects. We didn’t re-select the tag.

If you had tried to run the application with the tags set to default then you would see that no collisions would occur between enemies and lasers or the player.