Make player stop other object rigidbody2d with ontrigger collider that is on moving conveyor

Hey all,

I was going to ask a question about how to make this happen, but I think that I have figured it out myself in this instance, so I figured I would leave this as reference and also take feedback to better ideas/thoughts/strategies on doing this.

I have a player with rigidBody2D and a circle collider 2D. I also have bomb objects that have rigidBody2D and box collider 2D with isTrigger checked. I also have conveyor objects with box Collider 2D with isTrigger checked on them with a speed float and moveDirection Vector2.

For conveyor I have OnTriggerStay2D AddForce to colliding object in the moveDirection * speed of conveyor

For bomb I have OnTrigger code so that when they touch a certain other gameObject they explode and Destroy theirselves.

My issue was that since I had onTrigger on the collider2D on the bomb, my player and bombs would go right through each other. I wrote code in OnTriggerEnter2D and OnTriggerStay2D on the bombs to set their velocity to Vector2.zero but the conveyor would keep pushing them right on through the player.

Long story short I fixed this by adding another collider2D to the bomb, now the player and bombs don’t go through each other! Sorry, but I am kind of new to Unity and all the posts and tutorials I have read have only ever involved one collider on an object so I didn’t even initially consider doing this. If there is a better way though, please let me know!

By setting the Bomb as a trigger the two colliders no longer interact with each other with physics. In order for the RigidBodies to “push” on each other you need to uncheck isTrigger on the bomb, and use OnCollisionEnter2D instead.

Awesome, thanks! That makes a lot of sense now. Somehow I thought that to run custom code on collision that you had to mark the collider as on trigger. I think I got that stuck in my head because the first two tutorials I did years ago both had me mark it as on trigger and write code for OnTriggerEnter.

So when exactly would you use is trigger for a rigidbody instead of using OnCollisionEnter or OnCollisionStay?

Well you would use a trigger anywhere you want a collision to occur but not effect the motion of rigid bodies. For example you could use it to trigger in game events, such as when your player reaches a respawn location. You could have a sphere collider that he walks into which causes a sound to play and updates the save file with his new spawn location. You wouldn’t want your player actually interacting with this sphere, he just runs thru it.