Ive done some diggin around and am stumped…still tryin to wrap my head around coroutines…roll eyes now.
I have a bullet that i shoot and when it collides with and enemy it calls the function Combo(). The problem I am having is the yield part. I have read that a yield in a coroutine will halt the function at that point and resume on the line below next time the function is called…mine just rips right on through after first collison…what am i missing.
Here’s the code:
private var comboStartTime : float;
private var comboEndTime : float;
function Combo()
{
comboStartTime = Time.time;
yield;
comboEndTime = Time.time;
if (comboEndTime - comboStartTime < 3)
{
print("Combo");
}
}
Well, coroutines work differently Yes they are kind of “suspended”, but they don’t resume when you call it again. When you call it again it would start another coroutine.
See this documentation page.
A started coroutine is an object that is stored internally by Unity’s coroutine scheduler. This scheduler will take care of resuming your coroutine depending on what you yield in your coroutine.
In your case you yield “null”. This will make the coroutine wait for just one frame.
For what you want you don’t really need a coroutine. Just do it like this:
var maxComboDelay = 3.0;
private var lastShotTime = -10.0;
function Fire()
{
if (Time.time - lastShotTime < maxComboDelay)
{
// combo
}
lastShotTime = Time.time;
}