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

OnCollisionEnter2D vs OnTriggerEnter2D for Attack Detection

Discussion in '2D' started by TanselAltinel, Sep 8, 2015.

  1. TanselAltinel

    TanselAltinel

    Joined:
    Jan 7, 2015
    Posts:
    190
    Hi all;
    I've been using Unity for a long time, but I could say I'm relatively new to 2D.

    The project I'm working on is a 2D project, and for attacks between enemies and player I used 2D collisions. However, recently it started to give issues, like if an enemy or player attacks against an obstacle, it pushes back. Attacks between enemies and player cause weird jerks because collider pushes them.

    I played with collision layers, but now it became too much of a mess and I don't really want to play with it anymore.

    My question is, what do you use for attack detection?
    Should I switch to triggers instead of colliders?

    Regards, Tansel.
     
    Wachi555 likes this.
  2. TanselAltinel

    TanselAltinel

    Joined:
    Jan 7, 2015
    Posts:
    190
    Thanks for great replies :)
     
    Wachi555 likes this.
  3. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Hey there, you should definitely use triggers to solve your problem. Having the colliders as a solid will end up pushing things away from the solid (or towards in some cases).
    I create a gameobject through code and attach a BoxCollider2D and set it to a trigger. As far as I'm aware I think anything that has to be hit must also have a RigidBody along with it's collider, otherwise the OnTriggerEnter message does not fire.

    You can then use collision layers to fine tune your hitboxes to only hit certain objects. Here is a link to a collision chart I found online to help me figure what will hit what http://answers.unity3d.com/storage/attachments/19729-proof.png.
    As far as I have tested as well, kinematic RigidBodies do not return a trigger collision for some reason either.

    Hope that helps a little bit :).
     
    Wachi555, sam_nau and theANMATOR2b like this.
  4. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    I use a boxcollider2d for the body of my characters, a circle collider for the head of my characters, and a boxcollider2d with istrigger for the hitbox of my character. Then I use a raycast to determine attacks and such - I'm really happy with the results :)
     
  5. TanselAltinel

    TanselAltinel

    Joined:
    Jan 7, 2015
    Posts:
    190
    @Aedous, thanks a lot.
    Currently, my system works as intended, but some problems arise like pushing obstacles, and I'm at my limit of collision layer settings. Do you think if I change all my melee colliders to triggers and change OnColliderEnter2D to OnTriggerEnter2D have the same effect?

    @Rostam24, thanks, but I can't use raycasting for complex melee attacks.
     
  6. TanselAltinel

    TanselAltinel

    Joined:
    Jan 7, 2015
    Posts:
    190
    Well, I've switched all my OnCollisionEnter2D code to OnTriggerEnter2D.

    Worst thing I had to handle was getting information Collision2D provides but Collider2D does not. ContactPoint2D array was so useful, but I had to use RaycastHit2D for that information. So, it's as @Rostam24 said, raycasting gets me necessary info.

    I'm happy with results.
     
  7. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Nice one glad it worked out for you, if Raycasts get to cumbersome especially if your trying to be specific, try triggers instead on the character and on the melee attack. Good luck to you!
     
  8. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    Trigger Collider2D is good. You can have multiple colliders on one gameobject but you need to make sure they are on separate layers and stuff so just avoid that.
    OnCollisionEnter2D & OnCollisionExit2D
     
  9. TanselAltinel

    TanselAltinel

    Joined:
    Jan 7, 2015
    Posts:
    190
    I'm not sure how accurate it is, but the feedback I got that for fast paced movements and collisions, triggers give a better performance.

    So I might suggest to use that.

    @Aedous, that's an interesting idea, but for now everything works fine for me, so I won't touch that :D
     
    Aedous likes this.