Search Unity

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

Question 2D Box Collider - Fast Moving Triggers

Discussion in 'Physics' started by Weaver, Aug 19, 2023.

  1. Weaver


    Jul 2, 2013
    Hello, I'll give a rundown of my setup of this 2D game and what I'm trying to achieve.
    I'm using the 2D versions of everything (BoxCollider2D, rigidbody 2D, etc).

    Enemies are dynamically simulated rigidbodies with a BoxCollider2D
    Bullets are dynamically simulated rigidbodies with a BoxCollider2D

    Bullets are set to continuous and interpolation for their collision detection.

    For the bullets I set them to be a trigger, as I want them to be able to overlap and/or pass through the enemies without colliding with them and exerting a force on them.

    However, in doing so if the bullet is moving too fast it doesn't correctly interpolate the overlap only when set to trigger.

    I've experimented with setting both the enemies and the bullets to kinematic/dynamic in both combinations as well as setting trigger and non trigger.

    Non trigger using the OnCollisionEnter2D method will resolve the collision regardless of speed. However, using a trigger with OnTriggerEnter2D appears to be skipped unless there is a literal frame overlap in the FixedUpdate function.

    I'm at a bit of a loss what to do here - advice is welcome.

    My current ideas are

    1) Make everything kinematic then write a custom enemy collision resolver. I sort of dread doing this but I could give it a go. It just seems like a very rabbit hole situation. I experimented with my enemies and an avoidance flocking system and it was okay but lead to some jittering.

    2) Try using the BoxCollider2D cast to, effectively, do a sweep test.

    2a) Switch to the 3D collision system to get the rigidbody sweep test.

    My concern with a sweep test is I would need to figure out how to do it if the projectile is travelling at a curve.

    That said this definitely feels like something that should be doable. Maybe there's a simple solution staring me in the face.

    Help s very much appreciated, thank you for your time.
  2. MelvMay


    Unity Technologies

    May 24, 2013
    Triggers don't support continuous detection because continuous detection requires that there's an impact point; something which triggers don't have.

    Nothing in a physics simulation moves in a curve. It's always linear i.e. velocity * time-step. Multiple simulation steps per-second approximate a curve. So, perform a cast such as Rigidbody2D.Cast / Collider2D.Cast or approximate it with a raycast which is often much better for simple projectiles, especially if there's lots of them.

    Projectiles are nearly always better implemented as simple physics queries.

    I have no idea why you'd say this because 2D physics has way more options for sweeping anyway. It sort of implies that you think 2D hasn't got the available sweep tests.
  3. Weaver


    Jul 2, 2013
    Thank you for your reply and taking time on a weekend.

    I'll admit I never really thought about this, but it makes sense. Thank you, it's a good key piece of knowledge to model my understanding of the simulation.

    I said it because I haven't really used 2D physics in Unity. It was just a bonehead misunderstanding on my part cause in the 2D system it's called a "cast". I apologize for not knowing this - but of course I'm asking questions on the forums because I have incomplete knowledge. Reading my post again I don't really even know what I was getting at cause I knew the collider could cast, i just thought the rigidbody couldn't lol. I don't know - don't work too hard, it fries your brain.

    I actually went with the custom collision resolver approach and made a simple system with the colliders to push them away from each other - but I'll probably go back to dynamic bodies as the physics system already does this for me (and it does it better) and I'll use casts for the projectiles.

    Thank you again and have a good day.
    MelvMay likes this.