Using trigger volumes responsibly

As mentioned previously, we can treat our physics objects as normal colliders or as trigger volumes. An important distinction between these two types is that the OnCollider...() callbacks provide a Collision object as a parameter to the callback, which contains useful information such as the exact location of collision (helpful to position a particle effect) and the contact normal (useful if we want to move the object after the collision manually). However, the OnTrigger...() callbacks do not provide this kind of information.

As a result, we should not try to use trigger volumes for collision-reactive behavior since we won't have enough information to make the collision appear accurate. trigger volumes are best used for their intended purpose of tracking when an object enters/exits a specific area, such as dealing with damage while a player stays in a lava pit, triggering a cutscene when a player enters a building, and initiating asynchronous loading/unloading of a scene when the player approaches/moves far enough away from another major area.

If the contact information is absolutely needed for a trigger volume collision, then common workarounds are to do any of the following:

  • Generate a rough estimate for the contact point by halving the distance between the trigger volume and colliding objects' centers of mass (this assumes that they're of roughly equal size).
  • Perform a raycast upon collision from the center of the trigger volume to the center of mass of the colliding object (works best if both of the objects are spherical).
  • Create a non-trigger volume object, give it an infinitesimally small mass (so that its presence barely affects the colliding object), and immediately destroy it upon collision (since a collision with such a large mass differential will probably send this small object into orbit).

Of course, each of these approaches has its drawbacks—limited physical accuracy, extra CPU overhead during the collision, and/or additional scene setup (and rather hacky-looking collision code)—but they can be useful in a pinch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.211.134