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.

Creating self growing level

For my last game Project Pumpkin Jumpin 80 level had to be created. That made up a large part of the work. In addition, a level editor had to be created.

Because
the work on the game logic was much more fun to me, I decided that my next game will use automatically generated levels.

Although it was quite a bit of work, but now the first version of the self-growing city is ready.
The algorithm
is still not perfect: The density of roads is still too high and the street pattern still look too synthetic.

Here are the first animated screenshots:

 

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.

Decisions, Artificial Intelligence and Finite State Machines in Unity3D

After programming my first AI approach for the traffic system I've noticed that the reasons for the decisions of the vehicle are a little bit difficult to observe.

But for the future game I expect far more complex decision systems. So I needed a better debuggable solution than coding in normal C# methods.

After researching some hours I thought that “finite state machines” (FSM) could be a possible approach.

If found some implementations of FSM for Unity3D which were inspiring but not exactly what I needed:

So I decided to write my own finite state machine. It looks like this:

My FSM works by creating an instance of a state object for every available state.
Transitions from this state to the next state are attached to the state. 

For example:
The vehicle should follow another vehicle when touching it´s back bumper bar.
In this case the vehicle is running in the state “stateDrivingOnOthersBackBumperBar”.

As soon as no more touching the back bumper bar the state should be exited and the state “DrivingForward” should be entered.
So a transition “transitionNoMoreBackColliderTouched” has to be attached to the state. 

In code this looks like this:

// Create a new state for driving on others vehicle bumper bar
var
 stateDrivingOnOthersBackBumperBar = new StateDrivingOnOthersBackBumperBar(this); // Define the reason to activate to transition to change to state "DrivingForward" FsmTransition.IsFulfilled noMorebackColliderHit =
() => !this.MyPhysics.GetColliders<VehicleBackCollider>().Any();
// Define "back-collider-ended-touching" transition
var
 transitionNoMoreBackColliderTouched =
new FsmTransition(noMorebackColliderHit, MyFsmStates.DrivingForward) { Order = 1, Name = "no more touching others back-bumper-bar", };
// add Is-no-more-touching-back-collider transition stateDrivingOnOthersBackBumperBar.AddTransition(transitionNoMoreBackColliderTouched);

Ok, now it works in a different way than before. But it still cannot be better observed.

So the next thing I needed was a visual representation of what the finite state machine is “thinking”.

I wrote a Unity3D editor window to display this via a force-directed graph drawing:

 

See the complete FSM in action:


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.