The question or thesis

Most of Daniel Shiffman’s The Nature of Code can be translated into Unity/C# without much conceptual loss if the rendering stays simple. The important thing is not to copy the p5.js presentation literally, but to preserve the simulation model: per-frame state updates, simple rules, and visible behaviour emerging over time (Shiffman, The Nature of Code, see source-nature-of-code).

For this vault, the best teaching route is small 2D demos built from sprite squares, dots, or grid cells. That keeps the examples readable for students who are still learning Unity scene workflow as well as the underlying algorithm.

What the evidence suggests

The current source-backed cluster already points to a natural Unity order:

  1. Random walk and motion basics: discrete steps, update loops, and state over time
  2. Steering behaviours: velocity, acceleration, force, and smoothing toward a target
  3. Particle systems: many short-lived objects sharing one update pattern
  4. Cellular automata: discrete grids, neighbourhood rules, and double buffering
  5. Flocking: local interaction producing emergent group motion
  6. Genetic algorithms: populations, fitness, mutation, and selection

That order matches the pedagogical shape of the source material: simple per-agent motion first, then group behaviour, then rule-driven emergence, then optimisation and adaptation (Shiffman, The Nature of Code, see source-nature-of-code).

Current implemented batch

How to use these examples

Treat each example as a small laboratory rather than a finished game feature. Run one scene, identify the state that changes over time, then make one visible change before moving to the next topic. That sequence helps students connect the code to behaviour on screen.

For a first pass:

  1. Start with the random walker and watch how a single position changes over time.
  2. Move to seek and arrive steering to see how velocity and acceleration produce smoother movement.
  3. Use the particle system to study many short-lived objects sharing one update pattern.
  4. Use the cellular automata example to study a grid where every cell follows the same rule.
  5. Use the genetic algorithm text example once the difference between state update and visible output is clear.

Scene setup pattern

Most examples can be run with the same Unity workflow:

  1. Create a new 2D scene.
  2. Add an empty GameObject named after the example, such as GameOfLifeDemo.
  3. Add the scripts listed for that example.
  4. Add any required rendering component, usually SpriteRenderer.
  5. Set Inspector values to small, readable numbers before increasing scale.
  6. Press Play and confirm that one visible behaviour changes over time.

If the scene is blank, first check that the camera is pointing at the object, the script is enabled, required components are on the same GameObject, and Play mode is running.

Code walkthrough pattern

Read each example in four passes:

  1. State: find the fields or arrays that store the simulation, such as position, velocity, particles, population members, or cell states.
  2. Step: find the method that advances the simulation, usually Update(), Step(), or a manager method called once per frame.
  3. Rules: find the short block where the algorithm makes its decision, such as steering force, lifespan decay, neighbour count, or mutation.
  4. Display: find the code that turns data into something visible, such as transforms, sprites, colours, text, or textures.

That reading order stops the rendering code from hiding the algorithm.

What to change first

ExampleFirst safe editWhat students should notice
Random walkerStep size or direction choicemovement becomes smoother, wilder, or more biased
Seek and arrivemaxSpeed, maxForce, or slow radiusthe vehicle overshoots, glides, or stops more cleanly
Particle systemspawn interval, lifetime, or initial velocitydensity, rhythm, and trail shape change
Cellular automatawidth, height, aliveChance, or stepIntervaldensity and update speed change the pattern’s character
Genetic algorithmtarget phrase, population size, or mutation rateconvergence becomes faster, slower, or less stable

Debugging checklist

  • If nothing moves, check whether the script’s update method is being called.
  • If values change but nothing appears, check the rendering component and camera.
  • If behaviour changes too quickly, reduce speed, spawn rate, or step interval.
  • If behaviour looks random forever, inspect the rule or fitness calculation before increasing scale.
  • If performance drops, reduce object count before optimising code.
ConceptSimplest Unity representationWhy it works
Random walkone dot sprite or small squaremakes state changes obvious
Steeringone square or triangle spriteheading and velocity are readable
Particlespooled dot spritesclearly shows emitter + lifespan pattern
Cellular automataTexture2D or tile gridmaps directly to cell arrays
Flockingrotated spriteskeeps neighbour interactions visible
Genetic algorithmssimple agents plus target markersmakes fitness legible without extra art

Disagreements or tensions

There is a useful tension between the elegance of the book examples and the reality of Unity as an engine. In p5.js, drawing and simulation sit in one lightweight loop. In Unity, students also need to decide where to put logic: Update, FixedUpdate, coroutines, pooled objects, scene references, camera-space conversion, and prefab setup. That means the Unity version is slightly less minimal, even when the underlying algorithm is the same.

There is also a scaling trade-off. SpriteRenderer-per-agent examples are excellent for learning, but not for thousands of boids or particles. For this vault, clarity comes first. Performance-focused variants can come later as a second layer rather than replacing the readable examples.

What to investigate next

  • Add a Texture2D-based cellular-automata example for Game of Life and cave generation.
  • Add a simple boids scene to extend steering-behaviours from single-agent steering to flocking.
  • Add a minimal rocket or path-evolution demo for genetic-algorithms.
  • Compare manual transform-based motion with Rigidbody2D force-based motion in Unity, because the book’s simplified model does not always map directly onto engine physics.

Practice

Choose one implemented example and write a five-line lab note:

  1. What state does the example store?
  2. Which method changes that state?
  3. Which rule controls the visible behaviour?
  4. Which component or method displays the result?
  5. Which single Inspector value would you change first, and why?

Then change that value, run the scene again, and add one sentence describing what changed.

Self-test

  1. Why is the random walker a good first example?
  2. What is the difference between simulation state and display code?
  3. Why should a student change one variable before changing several at once?
  4. Which example best teaches double buffering?
  5. When should a readable SpriteRenderer example be replaced by a faster rendering approach?

Answers

  1. The random walker is a good first example because it shows state changing over time with very little surrounding Unity setup.
  2. Simulation state is the data the algorithm updates. Display code turns that data into something visible in the scene.
  3. Changing one variable makes cause and effect easier to see. Changing several at once makes debugging harder.
  4. The cellular automata example best teaches double buffering because it keeps the current generation separate from the next generation.
  5. A faster rendering approach becomes useful when the readable version is correct but the number of visible objects or cells is large enough to cause performance problems.

source-nature-of-code | procedural-generation | steering-behaviours | particle-systems-design | cellular-automata | genetic-algorithms | overview-unity-csharp-cpp-programming