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.
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 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.
There’s a lot more to know about colliders, but we need to discuss triggers for our purposes.
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.
The next physics component we need to discuss is the 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
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.
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.
And here’s the result!
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!