Rigidbody 2D and OnCollision vs OnTrigger

David Hunter Thornton
4 min readAug 19, 2022

--

How do I make my bullets “hit” the enemy?

Objective: Explain the Unity system for collisions and the difference between OnCollision and OnTrigger.

We now have two different types of obstacles for our Player Character to interact with from my last article. But at the moment, everything just passes through the player and the bullets. So let’s talk about Unity’s method for detecting when two objects have “touched”.

In the above example I’ve added a Circle Collider 2D to my “Enemy” Game Object. The Green Circle surrounding the object is auto-generated by Unity and will detect if any other colliders “touch” it.

An important thing to note for game feel is to ensure that your collider is just a little smaller than your object. You don’t want it so small that there is obvious clipping, but you also want the player to not be negatively impacted by a collider that is too big.

There is a small box labeled “Edit Collider” inside the component. This will give you access to points where the collider can be adjusted. (Repeat the above for the Mine, the Bullet & the Player using appropriate Collider shapes for each).

Now a collider on each of our objects isn’t enough to do what we want. We also need a Rigidbody2D. However, only the objects that are going to interact with another object do. As an example, we know the bullets are going to hit enemies and mines. We also know that the mines and enemies can hit the player.

An argument could be made for any of these game objects having Rigidbody2D; however, for my project, I’m going to put one on the Player and one on the Bullet. (Be sure to change the “Gravity Scale” to 0).

Now, we aren’t going to code anything yet. But we’re gonna talk about some of the important notes for how the collisions work in C#.

The Scripting API will be our go-to again here. If we look up “Collision” we’ll find a list of potential commands that Unity recognizes.

The best way to think about it is that OnCollision is typically used when you want one object to stop another object. And OnTrigger is if you want the two game objects to pass through one-another.

In the above example from the “Sly Cooper” series, the ground and walls are all using OnCollision because we want this to stop Sly from falling or moving through a wall. However, for the lasers, whether they hurt him or not, we want that to pass through.

With that in mind, we want all of our objects to pass through each other. If we were making a platformer then we’d want OnCollision for anything the Player Character could stand on as an example.

In my next article I’ll break down exactly how to use the above terminology in C#. Until then, go ahead and make sure to select “Is Trigger” under your Collider 2D Components for each Game Object.

Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9

--

--