- Home /
Invoke repeating limit?
Hello. Is there a way to set a limit on InvokeRepeating? For example, it calls a function every 2 seconds for 8 seconds? Thanks
Comment
Best Answer
Answer by Ejlersen · Dec 31, 2010 at 09:11 AM
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);
}
}
Your answer
Follow this Question
Related Questions
InvokeRepeating doesn't work 2 Answers
InvokeRepeating with a variable? 2 Answers
Invoke Repeating Rate 1 Answer
Pause InvokeRepeating? 1 Answer
Invoke.Repeating doesn't really "care" about repeat time? 1 Answer