10
\$\begingroup\$

I have a particular function which I am calling in Update function. For the first ten seconds, the function should get called in Update function, then it should get disabled for next two seconds, then again enable it for next ten seconds. This cycle should keep repeating? How can I execute it?

\$\endgroup\$

3 Answers 3

15
\$\begingroup\$

Alternatively, there is one-liner using modulus:

void Update()
{
    //if you want it loop from specific start time rather than from start of the game, 
    //subtract said time value from Time.time argument value
    if(Mathf.Repeat(Time.time, execDuration + sleepDuration) < execDuration)
        executeFunction();
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Is there a difference between Repeat() and % (modulus)? The documentation says "this is similar to the modulo operator but it works with floating point numbers", but modulus works with floats... \$\endgroup\$ Commented Jun 7, 2017 at 15:19
  • 1
    \$\begingroup\$ @BlueRaja-DannyPflughoeft yes and no - as far as I know in various languages the % operator often act weirdly - either doesn't work with floating point numbers, gives unexpected or outright incorrect results for modulus operation in its math meaning (reflecting hardware nature of the operation on integers). Repeat() was chosen just as a safer option to avoid necessity of looking up exact implementation of % operator in C#/mono. \$\endgroup\$
    – wondra
    Commented Jun 7, 2017 at 15:30
  • 3
    \$\begingroup\$ I thought every language implemented the IEEE 754 standard, but TIL the standard's "modulo" definition is unintuitive so almost no languages implement it. \$\endgroup\$ Commented Jun 7, 2017 at 16:27
  • \$\begingroup\$ It would be better if you implemented the comment over code as another code block and titled them accordingly. \$\endgroup\$ Commented Jun 11, 2017 at 11:11
5
\$\begingroup\$

I haven't tested the following code, but you will get the idea :

public float wakeUpDuration = 10.0f ;
public float sleepDuration = 2.0f;
private bool callFunction = true ;
private float time = 0 ;

void Update()
{
    time += Time.deltaTime;
    if( callFunction )
    {
         if( time >= wakeUpDuration )
         {
             callFunction = false;
             time = 0 ;
         }
         else
         {
             foo(); // Your function
         }
    }
    if( !callFunction && time >= sleepDuration )
    {
        callFunction = true;
        time = 0 ;
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ That should only work if deltaTime is relatively short. If the delta is longer than sleepDuration then this will fail. \$\endgroup\$ Commented Jun 7, 2017 at 13:40
  • \$\begingroup\$ if sleepDuration > Time.deltaTime, then, you don't need such system. \$\endgroup\$
    – Hellium
    Commented Jun 7, 2017 at 13:44
  • \$\begingroup\$ No. Say deltaTime is 2.75 seconds. You wil have 3 or 4 "active" calls then one "sleep" call, until the 50 second mark, when you should have 8 active calls in a row. Also, in that scenario, the first sleep call comes at 11 seconds -- you reset time to 0 -- it should be set to 1. \$\endgroup\$ Commented Jun 7, 2017 at 14:09
  • 2
    \$\begingroup\$ In Unity, Time.deltaTime represents the elapsed time between the current frame of the game engine and the last frame (thus, I hope for the final user that Time.deltaTime will never be 2.75 seconds, ....). In this context, you may "miss" a call to the function or a "non"-call to the function, which is not an issue here IMHO. \$\endgroup\$
    – Hellium
    Commented Jun 7, 2017 at 14:20
4
\$\begingroup\$

You can do this with a coroutine also. Something like

public class Comp : MonoBehaviour
{
  private bool _shouldCall;

  Start() 
  {
    StartCoroutine(UpdateShouldCall)
  }

  Update() 
  {
    if(_shouldCall)
        CallTheFunction();
  }

  IEnumerator UpdateShouldCall()
  {
    while(true) 
    {
        _shouldCall = true;
        yield return new WaitForSeconds(10);
        _shouldCall = false;
        yield return new WaitForSeconds(2);
    }
  }

}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .