Posted April 26, 2025 by Jingchen
#NavMesh #Scene
After designing my two main characters—the Plague Doctor and the Aerial Robot—I moved on to building a simple prototype inside Unity. The goal was to make both of them act like real agents, using basic navigation, sensing, and behavior switching. I didn’t focus on getting every detail perfect at this stage; instead, the priority was to create a working foundation where each character could move intelligently in the environment and react to simple conditions.
The first step was setting up the scene itself. I decided to use a cyberpunk-style city layout, filled with narrow streets and scattered buildings. One big challenge I faced early on was figuring out how to set up the NavMesh system properly. Since the Plague Doctor was going to walk around on the ground, I needed a walkable surface. In Unity, I selected the streets and the open areas of my scene, marked them as "Navigation Static," and then baked a NavMesh. However, because the city asset I used was very detailed (with lots of tiny props like boxes, streetlights, etc.), my first few attempts at baking the NavMesh were a mess—there were broken paths everywhere and the agent couldn’t move properly.
To fix this, I had to manually set many props like cars, signs, and small objects to have a NavMeshObstacle component with the "Carve" option turned on. This way, Unity would dynamically carve out spaces around these obstacles instead of treating them as part of the walkable surface. It took some trial and error (and a lot of rebaking) to finally get a clean and usable NavMesh that the Doctor could actually navigate without constantly running into walls.
Once the ground navigation was working, I created the Plague Doctor prototype. I imported the doctor model, added a NavMeshAgent component, and wrote a basic controller script. At this stage, I implemented two core behaviors (two states): walking and interacting. When idle, the Doctor would search for the nearest patient tagged in the scene and walk toward them. Once close enough, he would stop and trigger an animation that looked like healing. To make the Doctor behave according to a simple state machine, I used Animator.SetBool("isWalking", true/false) to switch between movement and idle states.
One technical hiccup here was that sometimes the Doctor would get stuck after reaching a patient. He would either hover awkwardly near them or start vibrating because the destination wasn’t recalculated properly. I later realized this was because the stopping distance on the NavMeshAgent was too small, and floating-point precision issues made him jitter. Increasing the stopping distance slightly and resetting the path after reaching a target helped smooth out this behavior a lot.
For the Aerial Robot, I took a slightly different approach. Since it flies, it didn't need to use the ground NavMesh. Instead, I made a simple flying script where the robot orbits around a moving center point. Like the Doctor, the robot also had a basic two-state logic: normally flying in circles, and descending when interacting with the Doctor. Every 15 seconds, the robot would leave its flying pattern, approach the Doctor, and both would pause for a few seconds to simulate a "conversation." I also added simple flying and landing animations to these states.
Getting the timing right between flying and landing was tricky. My first version of the robot would try to land while still moving too fast, causing it to overshoot or "snap" weirdly to the ground. To solve this, I tweaked the interpolation speeds and made sure it slowed down smoothly before touching the ground. I also added small look-at scripts so that when the robot hovered, it would naturally turn toward the Doctor, which made the interaction feel more alive.
Overall, building the first prototype was a messy but rewarding process. I spent a lot of time just figuring out how to make Unity's NavMesh and NavAgent systems behave the way I wanted. It also forced me to think carefully about scene design, asset setup, and simple state machines. Even though at this point the behaviors were basic, seeing the Doctor and Robot moving and reacting in the scene for the first time was a really satisfying moment.