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

Question Will an awoken trigger2d collide with anything in it on the same frame it wakes?

Discussion in 'Physics' started by luniac, Jan 27, 2023.

  1. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    606
    And then if I use LateUpdate() to turn to trigger off, will this approach guarantee that all collisions have occurred with the trigger first?

    I need to have a trigger turn on, collide with whatever is in it, and turn off afterwards.
    But I need to make sure it doesn’t turn off too soon or in future frames.
     
  2. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    606
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,789
    It's a completely automatic process that doesn't change how physics works, it only improves performance therefore you can ignore it.

    Physics doesn't run per-frame, it runs (by default) during the FixedUpdate so your reference to "LateUpdate" isn't really relevant.

    A collider wakes when there's a new potential contact on itself or any other colliders it's currently in contact with; known as a contact island.
     
  4. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    606
    I have a use case which involves enabling a collider, getting any immediate collision, and then disabling after.

    Since there is no way to know beforehand the number of collisions, I need to just wait till the end of physics update to turn off the collider right?

    Now since there is no latefixedupdate() function but there is a coroutine yield WaitForFixedUpdate, would just putting a function call there effectively call a function at the end of FixedUpdate()?

    this would guarantee that any collisions that could happen have occurred first, and since I’m enabling the collider outside of the physics loop, there won’t be any weird timing thing such as where I enable collider after all triggers have happened for that physics update, and then it’s disable the collider at the end of the update, and it’d look like no collisions were there.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,789
  6. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    606
    Can that be called outside of the physics loop to immediately check for overlaps, or do we still have to wait for a fixed update so the physics engine can register any overlaps for a newly enabled collider?
    In which case it’d be redundant since waiting for fixed update would do ontriggerenter events anyway, so I don’t need to manually check for overlaps.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,789
    All queries can be called whenever you like, that's the point of them.:)

    Raycast, shape casts, overlaps are all checks that happen when you call them. The physics system itself doesn't use them and they don't relate to what the simulation step does.
     
    luniac likes this.
  8. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    606
    Ok thanks
     
    MelvMay likes this.