Search Unity

Multiple Colliders, how to diferenctiate multiple colliders applied to a single game object?

Discussion in 'Getting Started' started by coWolves, Aug 3, 2021.

Thread Status:
Not open for further replies.
  1. coWolves

    coWolves

    Joined:
    Mar 22, 2019
    Posts:
    2
    I have multiple hit boxes applied to the same object but when Unity detects a collision for any of these hit-boxes, It sends a trigger signal, it treats all 3 hit boxes as if they were one. How can I detect collisions from individual hit-boxes; determine which of the 3 has been collided with? I've spent hours searching on the internet and so far the frustrating answer I found to this question (A way to side-step the problem rather than fixing it) Is to create child game objects to the game object in question. Assign colliders to each child object. Then program each child object to send messages to the main game object. I don't like this solution because 1: Receiving signals from individual hit-boxes sounds rather fundamental for a well-put-together engine like Unity to do. I cant image a well put-to-together game engine such as Unity can have the ability to determine if a hit box is being collided with but NOT the ability to specify which of several colliders is being collided with. 2: It sounds like it would drain the performance of your computer breaking and distrusting a single task onto several game objects. And 3: I don't like avoiding problems, and rather learn to fix them than to learn to avoid them. MultiCollidersUnity.JPG


    (How the switch works): I'm trying to make a switch that has 3 colliders on it. One collider to determine if the switch is about to be collided with by the players finger. And 2 hit boxes to determining if the switch is to be turned on or off. The main purpose of the first collider is to ensure that the player dose not accidentally turn the switch on, then immidently back off by colliding with both hit-boxes simultaneously. The status of the switch can only be determined 'once', After one of the two colliders have been hit, the status of the switch cannon be changed until the players finger has exited, then re-entered the larger hit box.

    The 2nd screenshot shows how my button works. It works just fine seems how it only needs one collider.
    (You'll notice that the tag being used for the players finger is "ham". Dont ask why, ham is just the tag being used for the players hand that's all). The switch is bassed off of how the button works.
    MultiCollidersVisualStudio.JPG
    And the 3rd screenshot shows the curent code for my switch, it needs work.
    MultiCollidersVisualStudio-Unfinished.JPG
    (I'm new to the forums so the screenshots might not be visible. Sorry if they aren't.)
    If their not, here's an external link that should work:
    https://postimg.cc/gallery/pVNVgph
     
    JACKRJ_ likes this.
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You know the answer, you just don't like it. My advice is to get over it. Don't fight the system, man.
     
  3. coWolves

    coWolves

    Joined:
    Mar 22, 2019
    Posts:
    2
    Alright, I'll do it, it just seems silly to me that the system obviously knows which of several objects are colliding but cant tell you which one in particular, just that the hit boxes are indeed being collided with.
     
    JACKRJ_ likes this.
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Oops. @hippocoder is quite right, and I led you astray. My apologies! I've always done it the other way, not least because when you want to do other things, like associate a different PhysicMaterial with each collider, you have to put them on separate sub-objects.

    But for what you're describing, you can just interrogate the collision data to find out which was hit.
     
Thread Status:
Not open for further replies.