Using coroutines to create tutorials in Unity 3D

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:

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.

  1. Create a co-routine and run it when the level starts.
  2. If a initial introduction is needed, this should be the first command inside the co-routine.
  3. Create a WHILE loop which waits till the next milestone event happens and contains a “yield return null;”.
  4. 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”.
  5. 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.
  6. 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 (!this.money < 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.

RetroGames e.V. in Karlsruhe

If it´s saturday night and you are in germany near Karlsruhe, don´t miss to visit the arcade videogames museum of retrogames e.v..


For only a small entrance fee you can play nearly 50 original arcade and pinball machines as long as you want.

My first visit to the museum was really fantastic and lot of fun.


The machines are lovingly restored and most fully functional.


There are all important games of arcade history like Donkey Kong, Pong, Pacman etc.. and newer games like Street Fighter, Star Wars or Tron Legacy.


For more information visit http://www.retrogames.info/

Pedestrian pathfinding part 2: Collision detection

I spent much time during the last weeks in programming the pedestrian pathfinding. After investing much work into the buggy implementation of evasion pathfinding algorithm and many hours of debugging now it seems to work.

Instead of the vehicle implementation (using Dijkstra pathfinding) for the pedestrian I use a jumpoint-pathfinding and a grid map. 

To prevent pedestrians from colliding with aother pedestrian, they mark their map pos as blocked (drawn red in the screenshot).



To synchronize between the vehicles and the pedestrians the vehicles project their actual position onto the pedestrian map (drawn black in the the screenshot). So no pedestrian can enter a field, a vehicle is driving across. 

Only for exceptional cases (when the blocking of the map has not prevented a collision) there is a front collider attached to each pedestrian.



Pathfinding seems to work fine now. So I can now go on with a little more exciting part of game development including graphic stuff and game mechanics.

First (very simple) pedestrian pathfinding

The first approach for pedestrian pathfinding is now implemented. I chose an optimized version of the A* pathfinding with the jump point search algorithm. (For the vehicle implementation I used the Dijkstra algorithm, but this seems to be too greedy for the pedestrian pathfinding).

Collision detection and animation are still missing so the walk looks a little bit like a robot on roller-skates.

First AI approach for the traffic system

With more than one vehicle on the road a little bit of artificial intelligence is needed. 

I decided to attach three Unity3D collider (with activated “trigger” property and one rigidbody component) to each vehicle:

  • An active front collider to detect if the vehicle is about to crash into another car or pedestrian
  • A passive body collider to be detected by the front collider of another vehicle
  • A passive back collider – also to be detected by another front collider

 

First idea of the collider and the AI behavior: