2D Galaxy Shooter — How to Make and Destroy Enemies using Unity’s Physics Engine

Marcus Cohee
4 min readAug 11, 2021

--

Hello all!

Today, I will be going over how to make and kill the enemy using Unity’s physics engine. I will go over new components and methods, like Rigid Body, Box Collider, and OnTriggerEnter.

Making our Enemy

Player and Cube

Make a new primitive cube, make its color to red, and change its scale to 0.75 on X, Y, and Z, then make it a prefab like with our laser.

Enemy Behavior

Let’s make a new C# Script, name it “Enemy”, and give it to the enemy prefab. Here, we will make the enemy descend on the player and if the player does not shoot the enemy before it reaches the bottom, then respawn back up to the top at a random spot above the screen.

  • Movement
Movement Down
  1. Create a variable = [SerializeField] private float _speed = 4.0f;
  2. Go to the Update method and add the code above. This just says that the enemy will go down, at the set speed, at 1 meter per second.
  • Respawn
Respawn behavior

This is saying that if the enemy goes below -5.5y, then respawn at 7.4f on the y axis at a random x value between -9.5 and 9.5.

  • Random.Range is used if you want a value that is between any two numbers.
  • Creating the respawn variable there helps us see what the if statement represents

Getting Ready for Collisions

We need to start by adding a new component and adjusting another one.

  1. Go to your enemy and look at its inspector.
  2. At the bottom of the inspector, click the “Add Component” button.
  3. Search for Rigid Body and add it.
  4. Find the newly added Rigid Body and uncheck “Use Gravity” since we won’t be using it in this game.
    - The component “Rigid Body” gives physics to that object.
  5. Now find the “Box Collider” component and check the “Is Trigger” box.
    - This allows the object to be triggered by another object for an action
  6. Do the same thing with the laser but this time, the box collider will be a capsule collider.
  • The player does not need a Rigid Body or trigger.

Setting Tags to our Objects

Tags are needed for the next step of collision.

  1. Select the player and head to the inspector.
  2. At the very top, there is a drop down menu named “Tag”, click it.
  3. Select the “Player” tag.
  4. Now, select the Laser prefab.
  5. Head back to the Tag drop down menu and go to “Add Tag…”
  6. There, hit the + button and name it “Laser”.
  7. Now you can select “Laser” on the drop down menu.
  8. Do the same thing for the enemy.

Using OnTriggerEnter to Destroy the Enemy and Laser on Collision

OnTriggerEnter is a method that’s called when two GameObjects collide, like the laser and enemy

Head to the enemy script and create a new method called:

  • “other” means that if the enemy collides with a laser,
    then other = laser.

In that method, let’s add an “if statement” that says that if the enemy touches a laser, then destroy the laser and enemy.

This is saying that if an object with a tag of “Laser” collides with enemy, then it will destroy the laser, then enemy.

  • If you add the “Destroy(this.gameObject)” first, then unity will destroy the enemy and script before it’s able to destroy the laser. This is because unity looks at the script from top to bottom.

Now when you shoot an enemy, it will destroy the enemy and laser!

Look at this, it’s actually starting to look like a space shooter game!

Next, I will go over how to create a simple lives system using “GetComponent”.

Thank you for your time!

--

--

Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!