Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question how to make multiple 2d Colliders behave as one collider

Discussion in '2D' started by kinglotrich, Oct 11, 2023.

  1. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    Hello, ı am making 2d platformer and there is head shot mechanic therfore everyone has 2 collider body and head like in picture.

    The problem is that while projectiles work normal because they hit only one target already, melee attacks can hit multiple target that is why they also hit same target twice.

    I actually solved problem by adding if(collider is capsuleCollider2D) filtrer but than when if melee attack hit target's head, they dont deal dmg. I want to triggrer collider only once per target no matter how much collider they have.

    the Other solvation ı make is making a list on targets so that dont count same target twice but it is not perfect. At first some attack deal dmg more than once to same target like quick double attack.
    Other problem I think having list for every attack cause performance issue so is there any way to make multiple 2d Colliders behave as one collider and trigger colliders once ? upload_2023-10-11_19-52-31.png
     
  2. JustinNaicker

    JustinNaicker

    Joined:
    Jan 4, 2023
    Posts:
    47
    To clarify, a melee attack is activating multiple times because it is hitting both the head and body collider?
     
    kinglotrich likes this.
  3. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,345
    What you may want to do is disable the weapon or attack collider for a small period of time (0.5 seconds or so, but it all depends on your attack speed) so if it hits the head collider, it disables and cannot hit the body, and vice versa. This would best be done by an animation event. Once that collider cooldown has hit, you enable the collider again.
     
    JustinNaicker likes this.
  4. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    yes
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,941
    In a literal sense there is the composite collider 2d, that can combine box and polygon colliders: https://docs.unity3d.com/Manual/class-CompositeCollider2D.html

    It can't do capsule or circle colliders from what I understand.

    Another solution is to move the responsibility into the enemies in how they manage being hit. One can assume that when it's been hit, it could ignore hits from the same source for the next few physics updates.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    That was indeed correct but in 2023.1+ it can. :)


     
    spiney199 likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    So I presume your script is on the GameObject with the Rigidbody2D so you get both Collider callbacks? Or you might have both colliders on the same GameObject? Either way, in your script you essentially want to deal with only one melee attack despite potentially having multiple callbacks?

    Various ways of dealing with this by ignoring subsequent melee attacks if you've already handled it.

    You can deal with this by having a simple bool flag which you reset in your "FixedUpdate" callback and set when you've handled it like so:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MeleeThing : MonoBehaviour
    4. {
    5.     private bool MeleeHandled;
    6.  
    7.     private void FixedUpdate()
    8.     {
    9.         // Let's allow some melee action again.
    10.         MeleeHandled = false;
    11.     }
    12.  
    13.     private void OnTriggerEnter2D(Collider2D other)
    14.     {
    15.         // No more melee this update please.
    16.         if (MeleeHandled)
    17.             return;
    18.      
    19.         //
    20.         // Do some cool melee actions...
    21.         //
    22.  
    23.  
    24.         // Enough melee this update.
    25.         MeleeHandled = true;
    26.     }
    27. }
    28.  
    NOTE: This is assuming running the simulation at the default FixedUpdate.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,941
    Oh well that's awesome. I was just going off what the docs say, despite using 2023+ in my latest 2d project (and loving a lot of the 2d improvements).
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    I updated the docs for 2023.1 onwards. Did I miss something? If so, let me know and I'll get it updated.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,941
    The manual page I linked to still only mentions box and polygon colliders even in 2023.1+ versions. Though I notice that it does mention all colliders in the scripting documentation.
     
    MelvMay likes this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    Not sure why that was missed, it was on the task to change but I've just kicked that off again. Thanks for the heads-up and sorry for the docs hijack @kinglotrich :)
     
    spiney199 likes this.
  12. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    should I download unity 2023 beta ? although it is beta. I was actually waiting for normal release.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    That's up to you. Only if you need to use Capsule/Circle with a composite. It's not clear (to me) from your question, exactly what the issue is.
     
  14. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    Also I need that system still differentiate if it triggered on circle collider or capsule collider

    I mean, if I use composite collider, does still

    "if ( collider 2d is capsule collider 2){...}"

    statement work fine ?
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    No, it's a single collider which is why I suggested what I originally suggested. Do what you're doing and simply check if you've already dealt with a melee attack. This is just logic, not physics really. This is, of course, if I understand what you want which I've already said a few times I'm not exactly sure of.
     
  16. kinglotrich

    kinglotrich

    Joined:
    Jun 2, 2023
    Posts:
    23
    in short I want to make headshot mechanic while having one collider or composite collider. currently my code is like

    private void OnTriggerEnter2D(Collider2D other)
    {
    if(other is CircleCollider2D(the head))
    {
    inComingDmg*=1.2f

    }
    }

    can you suggest another option with using one collider ? how can I detect if only projectiles hit head but also melee atack should not deal extra dmg when hit head
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    I already suggested how to do that above. You get a callback telling you the collider.

    Why are you asking for "one collider"?

    This is just script logic.
     
  18. LoupAndSnoop

    LoupAndSnoop

    Joined:
    Jun 23, 2023
    Posts:
    21
    If you want a headshot mechanic, you do not want a composite collider, because you NEED to distinguish between a head and body shot. Fusing them together with a composite collider (or anything else) makes it way harder to know which target you hit.

    I think your confusion is in the collision callback, which is just a script issue. My recommendation is to consider moving each collider to a separate child gameobject, and then separate scripts that know one is just getting a callback for the head, and one knows it’s getting a callback for body. I otherwise don’t know how you can know which trigger you hit from an OnTriggerEnter callback.

    Alternatively, make the two colliders non-trigger colliders, set all the force overrides to none (send no force, receive no force), then OnCollisionEnter, check which collider got hit to do your logic.

    Another alternative is to have both colliders present normally, and onTriggerEnter, you call headCollider.IsTouching(collider), to see if it is touching the head or not.
     
    Last edited: Oct 12, 2023
    MelvMay likes this.
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    FYI: Created a docs PR, approved so just waiting in the queue to land in 2023.
     
    spiney199 likes this.
  20. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,562
    PR has landed in 2023. I have no idea what the actual update schedule is for the web for 2023 so might take some time to appear.
     
    spiney199 likes this.