I’m trying to get my code to execute at certain frequencies.
So it’s rather simple:
time++;
if(time%40==0)
cycle2++;
In my other game engine, this executes my code every 40 frames, but It’s kind of random in Unity. What do I have to do in Unity to make this work?
check the Time properties in unity docx. This is what I had done to trigger function on specific time:
float m_time = 0.0f;
void Update()
{
m_time += 0.01f;
if (m_time >= CertainFrequency)
{ //do something..
m_time = 0.0f; }
}
Update is called whenever possible. This means that it is not called at a fixed rate.
If you want to call something every n frames, you can use Time.frameCount:
int n = 40;
void Update()
{
if( (Time.frameCount % n) == 0 )
{
// do something
}
}
But you can also use a floating value to call something every n seconds:
int fSeconds = 5.1f;
int fElapsedTime = 0.0f;
void Update()
{
if( Mathf.Repeat( fElapsedTime, fSeconds ) == 0 )
{
fElapsedTime += Time.deltaTime;
// do something
}
}
Sorry that didn’t format well…
//Spot positions
if((timeAll+(0*sFrequency))%(4*sFrequency)==0)
{vLine1=vMirror;cycle1++;}
if((timeAll+(1*sFrequency))%(4*sFrequency)==0)
{vLine2=vMirror;cycle1++;}
if((timeAll+(2*sFrequency))%(4*sFrequency)==0)
{vLine3=vMirror;cycle1++;}
if((timeAll+(3*sFrequency))%(4*sFrequency)==0)
{vLine4=vMirror;cycle1++;}