Search Unity

Help Wanted Compound collider for multiple sprites?

Discussion in '2D' started by MidniteOil, Jun 20, 2021.

  1. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    178
    This may be a dumb question but I have a sprite that consists of several individual components. I drew this and saved it in the Photoshop .psb format and dragged it into Unity and rigged it for animation.
    upload_2021-6-20_9-31-55.png

    This is my first time using a "composite sprite". In the past my sprites were a single image and I used a polygon collider.
    upload_2021-6-20_9-36-9.png
    I don't see any way to apply a polygon collider to the parent that would encompass all the child sprites and follow them as they move.

    TL/DR: My question in a nutshell, is there a simple way to have the parent detect collisions from any of the child sprites other than each of them notifying the parent manually from OnCollision2DEnter()?
     
  2. frozenmangotree

    frozenmangotree

    Joined:
    May 2, 2020
    Posts:
    14
    I dont think so. What most people tend to do, which is what I believe you are doing from my understanding is to create a parent gameobject containing all the body pieces inside it as children. Then give each child its own collider. Anyways, if you dont want to give each child piece its own script then you can always just give it, its own tag and detect collision from whatever is colliding with it and pass that information to the parent gameobject. This includes particle collisions, just give the particle system its own script and pass the data on to the parent gameobject via onparticlecollision from the ps script. But i mean its honestly not that hard just create a script called body segment, slap it on each piece and invoke an event EventHandler from the oncollision method call in each of those scripts. Subscribe your parent to the body segment events. If you need to pass collision data along in those events to be recieved by your parent you can also do so, just google that. You essentially pass on a data class along with the event that inherits from EventArgs. Or alternatively you can do the same thing with a static event EventHandler, and then you only need to subscribe onetime as the parent, and anytime a collision occurs in any of your body segments you can still retrieve the relevant data for that body segment if you pass the data as a class dirived from EventArgs. Just make sure to nuke/unsubscribe all your listeners, especially with static events or that can get pretty nasty. But yeah events would be your best bet with this kind of scenerio in any case because its the only way i can think of you knowing when a collision occurs in one or any of your children, or all (if using static events) from the parent class itself. Hope this helps.
     
    MidniteOil likes this.
unityunity