Search code examples
c#unity-game-enginecollision-detection

Unity | How to get multiple objects with Compound Collision Detection to work?


I'm currently attempting to use collision detection for target selection (auto attacking). I have two objects (i.e. a player and a tower), each game object has an object hierarchy as such:

Object Hierarchy

Here, the object has a:

  • Target Sphere used for alerting itself, changing its game-state from idle to attack mode when another object enters this sphere.
  • Hit Sphere which is the essential hitbox for the object to take damage/ come in contact with other objects Target Spheres.
  • Selection Sphere which does not interact with other spheres but is the radius in which an object can be selected by the user.

Based on this I have set each child object to have a sphere collider and set to a layer corresponding to the type of sphere it is. Below is the physics collision table currently being used. I only want the HitBox layer and Target Layers to be interacting with eachother.

Physics Collision Layers Tower Sphere Layers

Despite my best efforts, when running the game and having the player walk back and forth through the spheres, OnTriggerEnter() is never called. This method is located in the player and towers classes which are on the parent game object. Currently I have no rigidbody on either the player or the tower, but do have triggers on both of the both the player's and tower's hit spheres to trigger each others OnTriggerEnter().

So far I have tried:

  • Adding a rigidbody to one, or both of the two parent objects
  • Adding a rigidbody to one, or both of the child objects corresponding to the HitSphere / Target Sphere that I am testing the collision on.
  • Switching the Layer order for the Target Sphere Layer and Hit Sphere Layers
  • Switching the Target Spheres to have the trigger instead of the Hit Spheres.
  • Having both Target and Hit Spheres be triggers.
  • Manually moving the objects to collide instead of relying on the current movement system.

Lastly, Here is an image of the two objects colliding, each within each others spheres but not calling each other's OnTriggerEnter(). The Cylinder outline is from the NavMeshAgent Controller which I have also tried to remove and test with but also to no avail. :(

Intersection of Spheres Not Working

TL;DR: Trying to do compound collision detection on multiple game objects to no avail.


Solution

  • The answer is actually buried in your question.

    From the Unity docs found here:

    Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

    But per your post:

    Currently I have no rigidbody on either the player or the tower, but do have triggers on both of the both the player's and tower's hit spheres to trigger each others OnTriggerEnter().

    So, the quickest fix sounds like it would be to add a Rigidbody to your Towers. You could also add one to your player, but make sure your collision matrix excludes the player from the encircling trigger sphere colliders.

    And lastly, as per the docs, a trigger won't be triggered by another trigger.

    Fix those issues, and you should be seeing the behaviour you expect.