"if(Time.time < 5)" is used to create a consistent test of firing rate between varying framerates.
Here are the results of the tests of all of the recommendations you guys gave me: This tests the number of shots fired in a 5 second period with slower and faster framerates and a 0.01 time interval between shots:
Coroutine: Slow = 93 shots, Fast = 147 shots
Invoke: Slow = 130, Fast = 193
Original: Slow = 135, Fast = 293
FixedUpdate Original: Slow = 250, Fast = 250
// This is the Original test
void OriginalTest()
{
if(CanFire ())
{
lastFire = Time.time;
shotsFired ++;
}
}
bool CanFire()
{
return Time.time >= lastFire + interval;
}
// This is the Invoke test
void InvokeTest()
{
if(canFire)
{
canFire = false;
shotsFired ++;
Invoke ("invoke", interval);
}
}
void invoke()
{
canFire = true;
}
// This is the Coroutine test
void CoroutineTest()
{
if(canFire)
{
canFire = false;
shotsFired ++;
StartCoroutine(Wait(interval));
}
}
IEnumerator Wait(float waitTime)
{
yield return new WaitForSeconds(waitTime);
canFire = true;
}
}
Results:
The FixedUpdate method appears to be the only one that works independent of the framerate. However, that creates another issue, which is that FixedUpdate tends to completely miss Input sometimes.
Is there a solution for that problem?
I do want it to fire exactly when the player presses the fire button.
Thanks again, I really appreciate all of your responses.
↧