Hello. Is there a way to set a limit on InvokeRepeating? For example, it calls a function every 2 seconds for 8 seconds? Thanks
This is probably not the best way, but it should work:
public class InvokeMeRepeating : MonoBehaviour
{
// The time we started the invoke repeating
private float startTime = 0.0f;
// The time we want to spend repeating
public float duration = 8.0f;
// Test rigidbody
public Rigidbody projectile;
void LaunchProjectile()
{
// If this is called after our time duration has passed, then cancel
if (startTime + duration < Time.time)
CancelInvoke();
else
{ // else keep on doing stuff
Rigidbody instance = Instantiate(projectile) as Rigidbody;
instance.velocity = Random.insideUnitSphere * 5;
}
}
public void Awake()
{
// Record the start time and lets start repeating
startTime = Time.time;
InvokeRepeating("LaunchProjectile", 0, 2.0f);
}
}