I have a problem in Unity. I need a countdown in my project. But this countdown will count 2 times. For example, 3 seconds a job will be done, 2 seconds another job will be done and these works will continue.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Based in the example of use you gave where an object first turn right, then turns left:
Below I give you two timers which are executed sequentially: first one will last 3 seconds and the second one other one 2 seconds. Once one counter finish the other one will starts, this will be repeated in an infinite loop.
I am not sure if this is what you were looking for. In any case there are lot of questions already asked about timers in Unity. Check some of them: https://stackoverflow.com/search?q=timer+in+Unity
Couroutines (and more Coroutines)
Coroutines do exactly the thing you want to do.
You then start them off by calling in your
Start()
method (or where ever):Note that both countdown methods will do their thing forever unless one of three things happens:
StopCoroutine(...)
is called, passing in the reference returned byStartCoroutine
while(true)
loop)yield break
Also note that in the event both coroutines should resume at the same time (e.g. at 6 seconds) coroutine 2 will execute first (as it was started first), unless some other effect intervenes (e.g. one of the loops has another
yield
instruction, one of the loops is terminated, etc).