Space Shooter: Collision

Gerald Clark
4 min readJun 13, 2022


So we have enemies, we have lasers, we have movement, all thats left is EXPLOSIONS AND SOUND EFFECTS AND CINEMATIC CUTSCENES.. right?

Well.. first we need to define logic that will eventually lead to stuff like that. Before I do that, I want to go ahead and switch all the stuff I’ve made so far over to 2D objects, and then change the sprite on their sprite renderer to something a little more eye catching. To do this check out this article from the last time I made this game.

Day 10: From Prototype To Work Of Art | by Gerald Clark | Medium

Now the game looks like a game. Lets set up some collision logic now.

First and foremost. Anything that will be colliding with other things needs to have a Collider2D on it. For the Enemy, I have a box collider. I’ve sized the box collider to fit the enemy sprite.

I also have a Rigidbody 2D on it with it’s gravity scale set to 0. That way this Rigidbody 2D will not respond to Unity’s built in Physics system.

Here is the collision logic on the Enemy Game Object.

In Start() I’m using GameObject.Find() to find the Player game object and getting the Player component off of it to reference. I’ll get into why in a second.

You can see I have a method called OnTriggerEnter2D. This handles collision. The if statements here say that if the object colliding with this game object are tagged “Player” or “Laser” do something. If the laser hits it I set hit to true. I mentioned this in the last article. This stops the movement code from running now. So when I hit it, it stays still and then destroys. I may change this later to just automatically destroy when it is hit with a laser, but for now I wanted to create this logic to use as an example.

The other if statement saying if(other.CompareTag(“Player”) destroys this object as well, but also calls a function form ther Player script. That is why I needed reference to the Player script in Start(). Lets go there and check out what this method does. Double click the method and hit F12. This takes you directly to the implementation.

So here is the Damage method:

I have set an int type variable in the player class called _lives. Its set to 3 in start().

When this method is called it subtracts 1 life from the Player. It also handles what happens when the player has no lives left. It destroys this object, and calls the function I mentioned in the previous article from the Spawn Manager called PlayerDied().

A lot happens when things collide with the enemy. The laser also has to have some collision logic.

So when the laser hits an enemy it destroys itself. For now. Later I’ll get into setting up a points system. This function has a lot to do with that.

For now we have a game that you can destroy the enemies with the laser and the player can be destroyed as well.

In the next article I’ll dig into how to set up a power up system.

Until next time!



Gerald Clark

Father Game Developer Music Composer Sound Designer