Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I calculate accurately Time passed in seconds for C#

Discussion in 'Scripting' started by Ovredon, Jul 19, 2013.

  1. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Hey everyone.

    So I am much more experienced in UDK/Uscript. Just want to get that out of the way.

    In Uscript there are in built Timer functions you can use to give delays to triggers, create intervals or looping events with them.

    I cannot find something like that in unity, I'm sitting here thinking "Surely" there is something I'm missing.

    What I need is an accurate portrayal of seconds passed which I can assign onto a variable.

    So.. thanks in advance I appreciate any suggestions.
     
    ocimum likes this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Look up WaitForSeconds.

    --Eric
     
  3. Ben-Massey

    Ben-Massey

    Joined:
    Jun 13, 2011
    Posts:
    581
  4. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    TimeT is a float variable.



    TimeT += Time.deltaTime;

    Debug.Log ("TIMER:" + TimeT);


    My question is when TimeT says >= 2.0 will that have been 2 realtime seconds passed ?

    I'm looking at the log and testing with stuff and it feels like 2 seconds passes after it gets to 0.9 or 2.0 ... It might just be me or something I'm doing, also will fps lag greatly effect its accuracy
     
    Loud_Lasagna likes this.
  5. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    from what I just read WaitForSeconds() create a delay in the execution of any code below it. Is that correct or am I way off ?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Possibly, depending on what you have Time.timeScale set to.

    Pretty much. That was in response to "In Uscript there are in built Timer functions you can use to give delays to triggers, create intervals or looping events with them", which are things that WaitForSeconds would be useful for. Also look up InvokeRepeating.

    --Eric
     
  7. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    Alright thanks..

    To give a quick example though. My Timescale is set to default which I assume would be 1.0, am I right in saying that 1.0 second ?

    So the in game time is going at 1 second per real life second as apposed to if timescale was set to 2.0 it would be going at double speed ( 2 seconds in game for every 1 second in life).
     
  8. Ovredon

    Ovredon

    Joined:
    Jul 13, 2013
    Posts:
    161
    thanks to both of you though for the suggestions. It has improved my library of things to try in other situations aswell :)
     
  9. Jakeo-Spikez

    Jakeo-Spikez

    Joined:
    Mar 27, 2013
    Posts:
    8
    Lots of ways to go about this depending on what you're trying to do.

    You can store Time.time in a variable then do something like checking when Time.time = storedVariable + 5f to know when 5 seconds have passed.

    Use Time.deltaTime in the update function to count seconds since that is 1/framerate.

    Use the invoke method that Unity offers, http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html
    Invoke also gives you access to the IsInvoking method which has come in handy for me before. http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.IsInvoking.html

    waitforseconds() was already mentioned.
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I would discourage this technique, since Time.time is a 32-bit float, where the precision degrades as time progresses. That can be a problem for apps that are intended to run for days or weeks at a time, such as in kiosks.

    --Eric
     
    pedroarruda4991 and Lorrak like this.
  11. xiDando

    xiDando

    Joined:
    Apr 6, 2016
    Posts:
    4
    private float time;

    void Start () {
    time = Time.time;
    }

    void Update () {
    if(Time.time >= time + 1f){
    //code something
    time = Time.time;
    }
    }

    I'm not sure if this is what you mean and it's a bit crude...but this will make the code in the if function only execute if 1 second has passed (1f).
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Please read my post above.

    --Eric
     
  13. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    your better off to use deltaTime over time.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TimerTest : MonoBehaviour {
    4.     [SerializeField] private float _duration = 1f;
    5.     private float _timer = 0f;
    6.  
    7.     private void Update() {
    8.         _timer += Time.deltaTime;
    9.         if (_timer >= _duration) {
    10.             _timer = 0f;
    11.             // Do Stuff here
    12.         }
    13.     }
    14. }
    15.  
    16.  
    or if your use case is simple use a coroutine or invokeReapeating
     
  14. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    If (Time.deltaTime > 3 years since last response) DoOtherStuff
     
  15. kayb14

    kayb14

    Joined:
    Jun 12, 2016
    Posts:
    10
    This is my snippet for this, easily adjustable to seconds, minutes, hours, whatever you want!!!!
    Works easy and fine for me!
    But it may not be 100% accurate since I don't know weather the Invoke method depens on the fps!
    Anyways don't try to count milliseconds or smaller!


    Code (CSharp):
    1. public int minutes= 0; //your time
    2. public float interval = 60; //change to 1 for seconds
    3.  
    4. public void triggerThisFunction ()
    5. {
    6.      minutes = minutes + 1;
    7.      Invoke("triggerThisFunction",interval)
    8. }
     
  16. Dogeveloper

    Dogeveloper

    Joined:
    May 10, 2019
    Posts:
    8
    i know is a bit late but i think the easiest way is using InvokeRepeating every second to add 1 to a counter

    int counter;
    InvokeRepeating("Count", 1, 1);
    public void Count()
    {
    counter++;
    }