Search Unity

Detecting Fast 2d Triggers

Discussion in '2D' started by HeathC, Apr 17, 2017.

  1. HeathC

    HeathC

    Joined:
    Oct 17, 2016
    Posts:
    19
    I'm using playmaker to make a game where it's a runner... but for a younger audience so the character isn't moving all that fast... and I have oncoming "bullets" that I want to pass through the player and give a knock back.. so they are set to triggers... and they are huge... this isn't a size of trigger issue... and they are moving slowly as well... but apparently they are fast enough that sometimes these huge triggers pass right through the capsule collider on the character... I've tried playing with the physics settings for position and velocity iterations along with the baumgarte scale settings... but there are times these huge triggers are passing right through the character without detection... I have spent all morning now researching fast trigger detection and physics 2d settings... I can't seem to find anything that helps fast trigger detection, and so even after just sitting here and playing with the settings... it's still happening.. any advice?
     
  2. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,344
    You'll want to enable Continuous Collision Detection on the Rigidbody doing the detecting.

    Also, since you say they are moving slowly, it may be an implementation problem. If you want to share the code you're using to move your physics objects, the issue may be apparent.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,600
    Unity (Box2D) does not use continuous collision detection on triggers so they always use discrete so if you move fast enough or the two colliders in question are small enough then they can jump over each other.

    Continuous moves the body to the point of impact which doesn't make sense for triggers as there's no collision response or position correction.

    A workaround for this is to perform a simple cast on the projectile. You can cast the whole body or the specific collider in the direction of motion or from the last position to the current position. This is effectively what continuous collision detection is.

    Try using Rigidbody2D.Cast in a FixedUpdate on the script. You can set the direction to be the normalised velocity and the distance to be the velocity * Time.fixedDeltaTime.
     
    jeffreyschoch likes this.
  4. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    2,344
    @MelvMay

    I'm a bit confused here, you're saying that CCD will sweep to the next location internally, but won't detect triggers (Box2D only?), and the alternative is to manually sweep the shape? Shouldn't this be a factor of the "Queries hit triggers" setting or something?

    I would've expected CCD to detect any colliders/triggers that would normally be detected by an equivalent shape cast and fire the appropriate callbacks.
     
    Last edited: Apr 17, 2017
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,600
    CCD isn't about detecting things during a sweep of the Rigidbody2D but to move the Rigidbody2D to the first contact point of the sweep. Triggers will not do that for hopefully obviously reasons. Only non-triggers attached to a Rigidbody2D will cause a collision response and therefore cause the body to move to the contact point.

    CCD is NOT about detecting entering then exiting contact. It's about doing what I just said above and moving the contact point. CCD is about ensuring you do not get tunnelling. Tunnelling doesn't apply to triggers as you always tunnel through them.
     
    Last edited: Apr 17, 2017
    jeffreyschoch likes this.
  6. HeathC

    HeathC

    Joined:
    Oct 17, 2016
    Posts:
    19
    ok great... thx for the help... i've converted the projectiles to ray casts... will investigate circle casts since the triggers were all so big and now the detection point is a single line...

    again, i do not code, this is being made with playmaker, but getting it figured out... thx again
     
    MelvMay likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,600
    Good luck! Ask away if you need more help.