Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Details about Time.time

Discussion in 'Editor & General Support' started by milkshakeiii, Jul 28, 2017.

  1. milkshakeiii


    Feb 16, 2013
    Hello, I'm working on psychology experiments using Unity and we care a lot about the exact timing of parts of our experiment and when stimuli appear on the screen. So I have some questions about Time.time that I was hoping someone might be able to help me answer.

    Time.time returns "the time at the beginning of this frame." I am wondering how exactly is the beginning of the frame defined? Is it the moment that the frame is swapped into the front buffer (in OpenGL)? And if so, are there any insights as to how accurately this represents the true moment that the frame is displayed on the screen (especially when vsync is enabled)?

    Thanks for any thoughts anyone can offer!
  2. orb


    Nov 24, 2010
    Update() is run just before the frame is rendered. If your target is 60 you might be off by 16.67 milliseconds for each frame Update() has skipped for whatever reason. VSync or not, there's no guarantee you actually get your target frame rate.

    Time.time is the time since the start of the game. It's the number of seconds since start to the exact moment you read it. Also look at Time.deltaTime, which gives you the time the previous frame took. The true time of the previous rendered frame is somewhere between (Time.time - Time.deltaTime) and Time.time.

    If you want fairly exact timings for things happening on-screen it's important to not abuse the garbage collector. Allocate as much as possible up front and hang on to it to avoid stutter. Don't free up things during the scene and the GC won't kick in when you least want it.
    milkshakeiii likes this.