2D Shooter: Making Everything Work Again

Sabrina Windsor
2 min readJun 10, 2021

--

We left off last time deleting gameobjects and attaching our scripts to sprites in order to add some art to the game. Well, this also ended up making everything not work…

Fear not! All we have to do is make some adjustments and everything will be hunky-dory. So let’s fix it!

Objective: Convert enemy and laser to 2D and fix the scripts to communicate properly once again.

We will begin by deleting components.

Yup, on our laser and enemy prefabs, all of our current components are useless except for the one holding our script. The components we’re deleting will only work with 3d objects, so once they’re deleted we’ll add some more that will work.

They will be the sprite renderer, 2D box collider, and 2D rigidbody.

Add these and drag the sprites you want to use for the enemy and laser into the box next “sprite” in the renderer. Also make sure sorting layer is set to “foreground” for both of them.

Now you want to make sure gravity is turned off for both of them and the box “isTrigger” is checked.

Now to be able to detect collisions again, we need to go to the Enemy script and go to the method that deals with collisions. We want to add “2D” to the end of OnTriggerEnter, and Collider. This will let us access and enable 2D collisions.

We’re almost done. Let’s go back to the player object. Since we deleted the player object we don’t have the laser prefab attached to our variable anymore and thus, can’t instantiate it. Re-drag the laser prefab to the appropriate box in the script component, and it should be all set.

The final thing to check is the tags. The enemy will be searching for objects with the tags in order to interact and collide with them. Once the tags are all redefined, everything should be good to go!

--

--

Sabrina Windsor
Sabrina Windsor

Written by Sabrina Windsor

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!

No responses yet