Using coroutines to create tutorials in Unity 3D

Using coroutines to create tutorials in Unity 3D

Unity3D MiniTrue

When reading the first time about unitys co-routine concept I thought by myself: How can this be useful?

In the meantime I found out that co-routines are one of the most interesting features of unity 3d.

For my actual game project I use them for several purposes; the most useful is to control interactive level tutorials.

The game contains an advisor avatar:

MiniTrue advisor

It guides the player through the level and automatically appears when a milestone is reached and the next hint is needed.

Co-routines are the perfect tool to manage this kind of tutorial.

  • Create a co-routine and run it when the level starts.
  • If a initial introduction is needed, this should be the first command inside the co-routine.
  • Create a WHILE loop which waits till the next milestone event happens and contains a “yield return null;”.
  • What a milestone is depends on the kind of game you are working on. For my actual game project these are “opening a dialogue”, “selecting a specific object” or “reaching a special place in the level”.
  • When the condition of the milestone becomes TRUE, the WHILE loop will exit. In my game the next command after the WHILE loop invokes the advisor popup to explain the next step.
  • Then the next WHILE loop to wait for the next milestone follows – and so on.

Here is an example how such a tutorial code could look like:

protected IEnumerator HintPlayback(int moneyToAdd, int itemsToBuy)
    yield return new WaitForSeconds(4);
    ShowMessage("Please look at the pending tasks.");
    while (!this.tasks.AreOpen) yield return null;
    ShowMessage(string.Format("Please add some money - at least {0}$.", moneyToAdd));
    while (! < moneyToAdd) yield return null;
    ShowMessage(string.Format("Perfect. Now please buy at least {0} items.", itemsToBuy));
    while (!this.items.count < itemsToBuy) yield return null;
    ShowMessage("You have completed the tutorial.");
    yield break;

You can also skip one ore more hints if milestones are skipped: just check for both conditions (for milestone 1 and 2) in the WHILE loop of milestone 1.

Categories: Unity3D , MiniTrue

QR-Code dieses Beitrags

Dieser Blog-Artikel beinhaltet keine bezahlten Produktplatzierungen.
Bei Affiliate-Links oder Rabatt-Codes entstehen für den Leser keine Kosten oder Nachteile. Bei einem Kauf über diesen Link/mit diesem Code erhält der Betreiber eine prozentuale Provision und/oder gewährt dem Käufer einen Rabatt in angegebener Höhe.