Week 2: Pheromones!!
March 10, 2026
99 little bugs in the code, 99 little bugs. Take one down, patch it around, 127 little bugs in the code.
Fixes!
Thankfully, I managed to complete all the fixes I wanted to do last week.
First, ants used to skid while moving around. This turned out to be the easiest fix by far. The reason why ants were gliding around in the first place was because I had limited them to a maximum rotation speed. After getting rid of that and switching to Unity’s built in “LookRotation” function, they now promptly face in the direction of their velocity.
The second and third issue was that ants were chasing around “imaginary pieces of food” that doesn’t exist, and that they somtimes didn’t deposit food properly.
Trying to fix this led me down a rabbithole of bugs, fixes, and new bugs that arose from the fixes. The old system that I had in place relied on every ant keeping a list of food in their general area, always pathfinding to the closest one, and changing targets if their previous food was suddenly gone or picked up by another ant, or some other event. Note the phrasing on “some other event”. As you can imagine, this left a lot of room for edge cases. Eventually, after hours of tussling with bugs, the code I had was almost entirely replaced by a new system, where ants “pair up” with a piece of food as soon as they’re near one another through an ID system. Once paired, they become intangible to everything else.
Looking back, the new system I had a much simpler concept and implementation anyways. I should’ve started with that.
New stuff!
The first thing I built was walls around the simulation area. Once ants touch these walls, their velocity and the direction they want to travel in flips. Now ants won’t run off infinitely.
I also wrote a short piece of code that can place a ton of food (~1000) on the map in a square formation. This is helpful for…
Pheromones!!!
Previously, the ants had the following decision making regarding the direction they want to travel in:
1. If the ant is currently carrying food, pathfind back to base.
2. If the ant isn’t currently carrying food, but have been paired up with a piece of food, run towards it and carry it.
3. If none of the above, wander around.
This is great, but this doesn’t leave any room for teamwork between ants. That’s where pheromones, one of the primary ways for ants to communicate, comes in.
Originally, I was going to code it so that scouts would leave a trail of “to-home” pheromones as they venture out, then once they find food, following that same trail back while laying a new “to-food” pheromone for other ants to follow. However, after consulting with my outside advisor, I’ve learned that most ants won’t passively leave “to-home” trail in order to avoid sensory overload. I’ve also found out that even without pheromones, most ants can find their way home through a combination of landmark recognition and counting their own steps. Some ants can even use sunlight to form their own internal compass. After discovering this, I will also opt to only code the “to-food” pheromone.
Here’s how the pheromones can guide ants in their navigation process:
1. If an ant has found food and is carrying it back, lay out a trail of pheromones as they venture back to base. Each pheromone has an associated vector that points to the direction of food. While pheromones in real life don’t have an inherent direction, I’ve found that most ants still can differentiate which way to travel to end up away from their home (either through the sun compass mentioned before, landmarks, or seeing the direction of other food-carrying ant), so I figured this approximation isn’t too far off.
2. If the ant isn’t currently carrying food but have been paired up, run towards it and carry it (same as previously).
3. Otherwise, Ants can detect pheromones in their general vicinity, and will add up the vectors from all pheromones nearby. Pheromones farther away are weaker. Pheromones will also slowly decay through time and fade away. After both factors are taken into consideration, ants will use that vector as their direction, while adding a “randomness factor” from the previous script used to wander around randomly.
I personally considered this a fairly elegant implementation. It’s very easy to keep track of pheromones in its vicinity, and keeping the randomness factor means that if there are no pheromones nearby, ants will still wander around and explore.
With all that said, here is a video of the ants in action with all the fixes and additions. Note how the pheromones slowly guide the ants towards a more significant target, and fosters teamwork between ants.
Up Next:
No glaring bugs at the moment! I can focus my attention on coding new features!
Currently, the way they pathfind back home is by running back in a straight line. I hope to further evolve the complexity by making it so they can pathfind back even when there are obstacles in the way (while also making the map more complex)
I also hope to begin coding a predator that will seek out and attack ants. Nothing complicated so far, as long as its functional.
Finally, I’ll try to actually figure out how to upload my project onto github for proper documentation.
See you all next week!
Reader Interactions
Comments
Leave a Reply
You must be logged in to post a comment.

Hi Luke! This is so cool! I love your video and I think it’s amazing that this project is a chance combines coding with researching a super impressive insect.
I’m glad you we’re able to address the major bugs in your code this week. Solving one problem in your code and creating 50 more is definitely something I can relate to in my senior project journey so far.
I’m curious about how you made pheromones that are farther away “weaker.” What does this look like in your code and how did you decide how much weaker to make each one? Was this trial and error, or did you use a particular formula?
Hi Rinisha,
In order to make the farther pheromones weaker, I simply took the distance between the position of the pheromone and the ant, and divided the strength of the pheromone by that distance.
My week 3 blog has now been published; along with all the pheromone reworks, I also removed the feature that made farther pheromones weaker, and instead simply stuck with decay. This is mainly because this feature heavily prioritized closer pheromones and diminished farther ones. In the future, I hope to implement a system that scales the system in a more controlled way (perhaps using functions like logarithms?)
Hi Luke!
I really enjoyed that you are taking as many steps to being as accurate to true ant nature as possible! I don’t have a particular question but I love the ideas you have for the future!