Prototype: Enemy Collision

Introduction

We’re making good progress on our prototype so far. We now have a player and an enemy with basic movement, but our projectiles go through the enemy!

Yeah, it’s awkward. We’re going to fix that in today’s post. Let’s get started.

The Challenge

Our challenge is to introduce collision detection so that our projectile will destroy the enemy GameObject. Thanks to the furnished physics components by Unity, this is an easy task for the prototype.

A good place to begin is a quick overview of said components, and then we’ll apply them to our GameObjects.

Collider Components

Collider components provide GameObjects with a defined shape for physical collisions. They are invisible to the player and do not need to match the exact shape of a GameObject’s mesh. The more detailed and precise the collider is, based on the mesh, the more processor-intensive they become.

The least processor-intensive colliders are primitive collider types, like Box Collider. Sphere Collider, Capsule Collider for 3D and their 2D equivelents like Box Collider 2D and Circle Collider 2D.

There’s a lot more to know about colliders, but we need to discuss triggers for our purposes.

Triggers

For our challenge, we don’t want our projectile and enemy GameObjects to cause a collision that blocks their paths. We want them to be able to pass through each other and respond to that event.

Indicating that a collider is a trigger will do just that; it will use the physics system in Unity to simply detect when one collider enters the space of another without creating a collision. Perfect!

Setting a collider as a trigger is done via the Trigger property in the inspector.

Trigger Property

The next physics component we need to discuss is the Rigidbody.

Rigidbody

The Rigidbody and Rigidbody2D components put the attached GameObject’s motion under control of the Unity physics engine. Simply adding a Rigidbody or Rigidbody2D to a component will cause it to be pulled by gravity and cause it to react to collisions with other GameObject’s that have a collider.

You can read more about it here.

Enemy Projectile Trigger

Let’s put the theory to practice. Since our projectile is using a Capsule primitive, it was created with a Capsule Collider already attached. All we need to do is check the Trigger property.

We can add a Rigidbody component to either our projectile or our enemy for Unity to invoke trigger callbacks, so I’ll add it to the projectile prefab. We don’t want gravity to be applied to our projectiles, so uncheck "Use Gravity" in the inspector.

Projectile Rigidbody Use Gravity

Again, our enemy is a Cube primitive, so it already has a Box Collider component. Still, we need to make sure it is set as a Trigger collider for the Capsule Collider on our Projectile prefab.

On Trigger Enter

Unity will invoke a method called OnTriggerEnter, providing an argument of type Collider that will give you information about what collided with our GameObject. There’s a nice collision action matrix at the bottom of the colliders overview to help you know under what scenarios Unity will invoke collision methods.

For us to detect what collided with us, we can use Tags in Unity. I’ll create a new tag, Projectile, and assign it to the Projectile prefab. There should already be a Player tag provided by Unity, and I’ll assign that to our Player GameObject.

Now, here’s our OnTriggerEnter method in the Enemy script.

Code Enemy Script OnTriggerEnter

And here’s the result!

Destroy Enemy

Summary

We can now destroy enemy game objects with our projectiles. We may also want to respond when the player collides with the enemy.

We’ll cover that scenario in the next post!
Take care.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s