- Home /
yield WaitForTime(1); question
where would i place the wait for seconds ion this script so that only 1 bullit fires per second?
var OnCollideExplode : Transform; var shootForce:float; function Update() { if(Input.GetButtonDown("Throw")) { var instanceBullet=Instantiate(OnCollideExplode, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); }
}
You can't put a yeildWaitForSeconds in an update function because an update function is called every frame
Answer by Eric5h5 · May 12, 2011 at 09:17 PM
Use a coroutine instead of Update.
function Start () {
while (true) {
if (Input.GetButton("Throw")) {
Throw();
yield WaitForSeconds(1.0);
}
yield;
}
}
This is neat. I'm curious though, since this is still called each frame, what is the difference internally and performance-wise (if there is any) between this and using Update? i.e. could/should you do this for everything and never use Update?
@$$anonymous$$olix: It's only called every frame while waiting for input, but once the throw button is pressed, it waits for a second, during which time nothing at all happens (except whatever internal method is used to wait for 1.0 seconds, of course). So if you press the throw button a lot, then performance would be slightly better, since it wouldn't have to run an Update function and associated code every frame. Overall it's more about using code that's easier to write and maintain. Update is still useful, but for code that involves various ti$$anonymous$$gs, coroutines usually make more sense.
Answer by Bryan 4 · May 12, 2011 at 09:09 PM
add a timer. function Update()
{ if(Input.GetButtonDown("Throw") && Time.time - fireTimer >= 1.0f) { var instanceBullet=Instantiate(OnCollideExplode, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); fireTimer = Time.time; }
}
this will make it so you can only fire once per second. (change the 1.0f) to whatever if you want different timings.
Answer by DaveA · May 12, 2011 at 09:10 PM
var OnCollideExplode : Transform; var shootForce:float; var lastShootTime = 0;
function Update() { if (Input.GetButtonDown("Throw")) { if ((Time.time - lastShootTime) > 1) { var instanceBullet=Instantiate(OnCollideExplode, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); lastShootTime = Time.time; } }
}
Thanks guys for this but i wasn't really clear i don't think basicly i need absolute decimal point precision ti$$anonymous$$g on this any idea?
1.0, 1.00? Update is called for every frame and unless you're machine is really old, it should be like 60 or more times per second, so it should be within .01 or .02 of that mark. You could look into the $$anonymous$$SDN for better times. OnGUI is called several times per frame, so it might have more precision that way.
Your answer
Follow this Question
Related Questions
How to delay function Update execution? 2 Answers
30 Objects all firing at exactly the same time and not randomly 1 Answer
Can IEnumerator be called more than once? 1 Answer
How to create a delay in a function being called in update? 2 Answers
yield WaitForSeconds to delay the instantiation of a game Object; 1 Answer