Question about 2D collisions. Best-practices for non-rigidbody collision.

I’m making a silly little tower defense prototype to learn the Unity3D system. I’m using the ex2D plugin for 2D animation. I’m feeling quite confident with the 2D system. Currently, when a mob walks into a collision box for a tower I add them to a List using OnCollisionEnter, then using OnCollisionExit I remove them from the list (or if the tower kills them, i remove them from the list).

This works well except for the fact that the collision system requires rigid bodies and the bodies end up colliding and causing undesirable effects. I have no need for collision detection to make 2 bodies repel each other. I want my 2D sprites to not “collide” with anything. I just want to know when mobs get within the range of the tower.

I’d like to be able to detect collisions without using any of Unity3D’s physics/rigidbody system at all. I would truly appreciate if someone would point me in the right directions for a best-practices approach.

I’m a career software developer but am new to Unity. Thank you for pointing me in the right direction.

If you want to get collision messages without objects actually colliding, set your colliders to “Is Trigger” in the Inspector and use OnTriggerEnter/OnTriggerExit instead of OnCollisionEnter.

Precisely what profanicus said. The IsTrigger option on the RigidBody disables the default collision response of the rigid body permitting you to supply your own solution.

Collisions in games are made up of two components, collision detection and collision response. Collision detection is determining that two or more bodies have or are about to penetrate. Collision response is deciding what to do about it.

The default collision response in Unity3D is of course the physics system that makes stuff bounce around but the collision response you want are the towers detecting the presence of a creep or mob and recording its presence. It is still collision response, per se, it is just a different type of collision response.

Thanks a lot, guys. Working great. My mobs have ‘rigid body’ that neither are kinematic nor use gravity. My tower has no rigidbody but has a box collider set to ‘istrigger’. Does that seem a reasonable solution?

Yep sounds good, as long as your tower is not moving - as you most likely know, a moving collider without a rigidbody is expensive. And if your mobs are purely under code/animation control (no physics at all) they should be set to kinematic.

Yes, sounds reasonable.

General rules of thumb are: “moving objects that require collision detection” get a collider and a rigid body. Non-moving objects only get a collider. Objects that should not respond to physics are set as IsTrigger. Objects that are moved directly through code and not forces should be Kinematic.

As profanicus said, colliders that move without rigid bodies attached are a compute expensive decision, though again, it is a rule of thumb that you can break in certain situations.

I appreciate all of your input. Thank you.