Coroutines of Unity

1 minute read

Recently I’ve stumbled onto a tutorial on using Linear intERPolation & Spherical Linear intERPolation in Unity.

That appeared to be a nice small one tutor, where you pause it many times and open your Unity and try!

Results

Here is a first result with LERP move: script control LERP move of a GameObject through checkpoints.

Side steps

Coroutines

One side step in that tutorial was to use coroutines (Unity) to run the transformations. And that was an interesting side step to see a good case for using them.

Here is a nice reading on Wikipedia about coroutines.

It is interesting also how example in Wikipedia tells it all, I’ll put it here:

var q := new queue

coroutine produce
    loop
        while q is not full
            create some new items
            add the items to q
        yield to consume

coroutine consume
    loop
        while q is not empty
            remove some items from q
            use the items
        yield to produce

call produce

And how example in Python docs tells no added value. Check what it shows:

>>> import asyncio

>>> async def main():
...     print('hello')
...     await asyncio.sleep(1)
...     print('world')

>>> asyncio.run(main())
hello
world

That boolshit does not make any sense, compare it to straight:

def main():
    print('hello')
    time.sleep(1)
    print('world')

main()

If you have no idea why to use coroutines, you’ll have hard time finding that out from Python docs!

Some more refs

  1. PEP 492 – Coroutines with async and await syntax

Updated: