While Susan and I were in Yellowstone, our Unity developer Tommi was working hard to make our bigger, better elk flock together realistically when fleeing predators. The video shows the results, and I couldn’t be more delighted. It’s just a thrill to run with the herd, even if they’re running away from me. (Note: I stripped down the terrain so it’s very basic in this video. But you can see the new controller in action. Though it needs some polish, right-click orbits the camera so you can look around easily while standing or running.)
Tommi’s the one to tell you how he did this, so let’s hear from him:
What about attacking those elk as they flee? We’re working on new mechanics for that as well. Stay tuned for more!Creating this herd AI was a very interesting challenge, and I'm glad that Dave gave me the time to develop it as far as what you see in the video.
The algorithm the AI is based on is an old one: Boids (https://en.wikipedia.org/wiki/Boids) has been around since 1986, and it's still the go-to method of creating herd/flock/swarm AI. What I've been doing is tweaking and modifying that system to match the specific kind of pattern created by a running elk herd. What's beautiful about this is that you don't need any code orchestrating this from above - instead, every animal thinks entirely for itself!
At all times, each elk in the herd tries to move in such a way that:
1) it moves in the direction it wants to go. Usually, that is away from you.
2) it stays close to other elk, because it's safer in the herd.
3) it keeps a certain small distance from its friends, to avoid stepping on their toes.
4) it moves more or less in the same direction as everyone else, because that helps with staying in the safety of the herd.
While it sounds easy, figuring out how exactly everyone should mix these different goals was pretty difficult. For example, the standard Boids algorithm tends to create a round herd because rule 2 above just pulls everyone towards the center. To create the nice long line you see, I had to create a kind of a "follow the leaders" logic instead. I also spent a lot of time trying to figure out how to prevent said leaders at the front from circling back on their buddies, or from splitting the herd up. Another tricky thing was getting elk that had been separated for whatever reason to nicely integrate back into the herd without running against the stream and causing chaos. While at the same time keeping everything responsive enough to outside influences such as wolves, who have an annoying habit to disrupt perfectly good formations.
In addition to the Boids stuff, I also improved the way the animals move in general. They now have a mass and have to accelerate and decelerate, and can't just suddenly turn 180 degrees at full speed. This, I believe, gives the stream its naturalistic, flowing feel, where you could easily imagine getting trampled under their feet.