Unity – Lab 3: Fireflies


Download the code package for the lab here.

Recall the following diagram from the reading on fireflies:


Feeling stuck? Here is a further description of the algorithm by Marton:

  1. Using the variables given, fill in the two blanks below with the options provided to complete the part of the code in the Charge() function for when the firefly should send the message to flash.

        • Your options are:
  1. The following diagram describes the flow of the firefly program. Arrows represent calls from one function to another.  Answer which of the following functions fits in each blank space.

      • Your options are:
      • (ignore the “IEnumerater” before each one. We will explain what that means during lab)


Part 0: TA Demo of Coroutine

Part 1: Flashing behaviour

  1. Open the ‘Firefly Testing’ scene
  2. Open FireflyLights.cs
    • All the code you will be writing today will be in this file
  3. Complete the TODOs for the following functions. Take a look at the SendMessageToFlash function to get an idea of what these functions should look like. Note that we won’t be completing the sawFlash function until part 2.
    1. Charge function
    2. WaitToCharge function
  4. Test your firefly
    • At this point, your firefly should be flashing at a steady 1 second rhythm (might be slower on laptop). Make sure this is working before moving on to part 2

Part 2: Synchronizing behaviour

  1. Implement the code for SenseFlashes function
  2. Test that sawFlash is working by clicking your left mouse to simulate it seeing another flash
    • For this part, it may be helpful to increase the delayMultiplier so things move a bit slower. You can find the slider for this in the inspector when you have the flock selected.
  3. Show your TA the functions you implemented in part 1 and 2


Part 3: Flocking behaviour

  1. Open the ‘Firefly Flock’ scene
  2. Press play and take a moment at your pretty flashing behaviour which you implemented in parts 1 and 2.
  3. Observe the movements of the fireflies in the scene.
  4. Click on the flock object in the hierarchy.
  5. Look at the flock script variables in the inspector
  6. Experiment with changing the sliders around as the program is running. Think about what each slider is controlling. What changes about the movement when each slider is changed?
  7. Look for 2 or more distinct emergent behaviors and note the flocking behavior variable values for each one. Show one of the behaviors to your TA and tell your TA what you think each slider is controlling.


Remember to include your code as part of your submission

  1. Compare the firefly to the ants in this video. How are the behaviours of the ants similar to, and different from the fireflies? (100 words or less)
  2. How are the flashing and flocking algorithms both examples of emergent behaviour? What are the lower level rules and higher level emergent behaviour of each?(100 words or less)
  3. (Optional) Please fill out this survey with your feedback on the lab, so we can continue to improve the labs in the future.