My 90 day journey becoming a Unity game developer: Day-12

We will now convert our enemy into a 2D game object. We will use a different way to convert our 3D enemy into 2D by opening the Enemy Prefab. Remove the following components: Mesh Filter, Mesh Renderer, Box Collider, Rigidbody. These components are for 3D objects which we are not going to use.

Removing components from the 3D Enemy to convert into 2D.

Add the component Sprite Renderer to display our enemy’s ship. Once the renderer is added, drag the enemy’s sprite onto the Sprite slot. Also, change the Sorting layer to Foreground.

Now add a Rigidbody2D to the Enemy’s Prefab. Make sure to change the Gravity Scale to 0 to keep the enemy’s ship from using gravity because we will apply it through our script.

Next component to add is the Box Collider 2D. In the enemy collider we will check off the Is Trigger box to allow an action to happen when the enemy’s ship collides with another game object. Plus, we will adjust the collider’s shape around the enemy and the player using the edit collider button.

When we play the game, we will notice that the game objects are not colliding at all.

Colliders doesn’t work even though we have colliders on the game objects.

The reasoning for this is that we must change the collider’s code in the script to OnTriggerEnter2D and Collider2D to use Unity’s functions on the 2D objects.

Changing OnTriggerEnter and Collider from 3D to 2D.
Enemy and Player colliders working, but not the lasers.

Finally, we need to convert the lasers to 2D. Let’s drag the Laser sprite onto the Hierarchy and add a Box Collider2D, Rigidbody2D, and attach the Laser script to it.

Scale the Laser down to 0.2 on each axis, and change the Tag to Laser. Under the Sprite Renderer, change the sorting layer to Foreground. In Box Collider 2D, click on Is Trigger, and click on the Edit Collider to adjust the collider to fit more uniformed around the Laser. Under the Rigidbody 2D component, change Gravity Scale to 0. Go over to your player script, and drag the Laser Prefab to the laser prefab variable’s slot.

Box colliders Is Triggers turned off.

If you look at the demo above, you will see with the triggers turned off in the Box Collider 2D the lasers are flying around sideways. The reason for this is that the player is doing surface collisions. Instead, we need our collisions to be pass-through collisions. For this reason, make sure to turn on Is Trigger inside of the colliders for the Player, Enemy, and Laser.

Is Trigger turned on.
Gameplay with all triggers turned on inside each collider.

We need to adjust the Laser’s offset, so the laser doesn’t shoot out from inside the player’s ship.

Finding the laser’s offset to adjust where the laser’s shot will be instantiated from.

Go to the player’s script and inside the FireLaser() method, change the laser’s offset from 0.8f to 1.05f.

Laser’s offset is adjusted for each shot to be instantiated in front of the player.

Next time we will work on creating a Triple Shot feature.



Learning to become a Unity game developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store