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

OnTriggerEnter before FixedUpdate

Discussion in 'Scripting' started by danBuonocore, Jul 14, 2016.

  1. danBuonocore

    danBuonocore

    Joined:
    Aug 26, 2015
    Posts:
    12
    This is a rather simple question but I just want to make absolutely sure. According to the docs, FixedUpdate is called immediately before the physics calculations. Does this include OnTriggerEnter?

    So if I want to check the results of triggered collision, I can enable my collider, wait one frame in the FixedUpdate method, and then by the second time FixedUpdate is fired, I can safely assume that OnTriggerEnter would have already been called if a collision was registered, correct?

    It seems to be working. After some testing I have yet to experience a time where OnTriggerEnter did not have the opportunity to fire before the second FixedUpdate, but I just want to be sure this is a reliable approach. Thank you!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    I don't really know, but this is easy to check. Print Time.fixedTime in both FixedUpdate and OnTriggerEnter. Time.fixedTime is the current time of the physics engine, and is the same throughout the entire fixed frame.

    So you'll get the same fixedTime in a FixedUpdate and a OnTriggerEnter. If you get that time first in FixedUpdate, that happens first. If you get the same in OnTriggerEnter, that happens first.
     
  3. danBuonocore

    danBuonocore

    Joined:
    Aug 26, 2015
    Posts:
    12
    Thanks for the reply! I've already tested it and the order is exactly as I was expecting. My question is can I assume the order will always remain this way? Or is there some sort of multithreaded process in the background that can behave differently in different circumstances?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    No, all of the engine code is single-threaded, and the order of the messages should be constant.

    so, yes, you should be able to rely on the order.
     
  5. danBuonocore

    danBuonocore

    Joined:
    Aug 26, 2015
    Posts:
    12
    Great! Thank you!