Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How it is possible to check the hit of the collision but without blocking the moving?

Discussion in 'Physics' started by The_wARmAcH1n3, Nov 20, 2023.

  1. The_wARmAcH1n3

    The_wARmAcH1n3

    Joined:
    Aug 5, 2023
    Posts:
    64
    I made in 2D a flamethrower. My problem is, that if a enemy jumps in the air and is over the flame, the flame blocks the moving of the enemy and so the enemy is riding on the flame but he should falling down through the flame. How can I change that, so that the collider of the flame registry the hit but doesn't block the movement?
     
  2. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    72
    I think you'd want to change the collider to a trigger collider, and use OnTriggerEnter2D instead of OnCollisionEnter2D.
     
    arkano22 and zulo3d like this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    757
    codebiscuits is correct but you may find it better to use OnTriggerStay2D so that your flame is constantly doing damage to the enemy while the enemy is touching the flame.
     
    codebiscuits likes this.
  4. The_wARmAcH1n3

    The_wARmAcH1n3

    Joined:
    Aug 5, 2023
    Posts:
    64
    Thanks, for the answers.

    My problem is if I use "OnTriggerEnter2D" so the class "Collider2D" is checked, but the Collider class have no contact points, which I need to know the position where I can spawn an explosion. If I use "OnCollisionEnter2D" so the class "Collision2D" is checked, which have the contact points I need for the position of the explosion.
     
  5. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    72
    I think probably someone will come along with a better solution, I'm not really experienced with 2D, but if not:
    https://stackoverflow.com/questions/31641522/how-to-get-contact-points-from-a-trigger
    ^^^Sounds like it will work.
    Otherwise, in 3D I think I could use contact modification and catch OnCollisionEnter (so get contact points; or just cache them inside the contact mod callback) but tell the physics not to react to the collision. I'm not sure that level of contact modification exists in 2D, though, and it feels like kindof overkill:)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,091
    In Unity 2022.3+ you can select (by layer) the direction forces (impulses) are applied when a contact occurs using:

    https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Collider2D-forceSendLayers.html
    https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Collider2D-forceReceiveLayers.html

    So, let's say the "flame" is on a "weapon" layer then you can go to the Collider2D of the player and turn-off the "weapon" layer on forceReceiveLayers. You can also turn-off forceSendLayers to the "weapon" layer if you don't want the player collider to send force to the "flame" either. You'll get the OnCollisionXXX2D callbacks as usual.

    Turning off both send/receive of a layer is essentially the same as making it a trigger as the contact is calculated but during solving the calculated force isn't used. This is more expensive than a trigger as it's calculating the contact-points and trying to solve them but does nothing with them.

    As with all the features, there's a dedicated quick example scene in my PhysicsExamples2D repo here: https://github.com/Unity-Technologi...s/Colliders/Collider2D_ForceSendReceive.unity
     
    The_wARmAcH1n3 and codebiscuits like this.
  7. The_wARmAcH1n3

    The_wARmAcH1n3

    Joined:
    Aug 5, 2023
    Posts:
    64
    Thank you, I will look into this.