← Back to Portfolio

Simulating City Commutes: GOAP Agents, Buses, and NavMesh Tracks in Unity

Published on July 25, 2026

Creating believable civilian routines adds immense depth to any simulated world. Moving beyond basic wandering scripts, this experiment dives into a fully functional commute system driven by Goal-Oriented Action Planning (GOAP). By establishing intrinsic needs—such as the requirement to sleep at home or be present at work—agents dynamically evaluate the most efficient sequence of actions to satisfy their goals. This system allows them to autonomously decide whether to walk to their destination or wait for a bus based on distance and spatial tracking.

At the core of this system is the Agent architecture. Instead of rigid behavior trees, the GOAP setup relies on modular action scripts assigned directly to the entity. Looking at the Unity Inspector, each humanoid agent is equipped with distinct, individual states: Action_Sleep, Action_Work, Action_Walk To Work, and Action_Take Bus To Work. An Agent Activity Tracker continuously monitors their schedule, working in tandem with the NavMesh Agent to trigger physical movement only when a state shift demands it. This creates a highly flexible logic loop that handles dynamic interruptions effortlessly.

Unity Inspector showing Agent activity and state components

To introduce complexity to the pathfinding, I implemented a public transit system anchored by the Bus prefab. The bus acts as both a moving platform and a state-modifier for the agents. When the GOAP planner determines that taking the bus is the optimal route, the agent navigates to the nearest designated stop. Once boarded, the individual agent's NavMesh logic is temporarily suspended, and they inherit the transform of the bus until they arrive at the drop-off node, saving significant computation overhead.

Bus prefab in the Unity Editor

The infrastructure supporting this transit system relies on a customized track and waypoint layout. Built over a tactical grid, the roads utilize curved splines with designated interaction nodes. These nodes function dually as navigation targets for the bus's route and spatial anchors where agents queue. This structured track ensures the bus model strictly adheres to the road curvature without relying on expensive, continuous physics calculations, keeping the simulation lightweight.

NavMesh Track routing and splines over the city grid

One of the primary technical challenges was efficiently syncing the agent decision-making with the transit timetable. If a bus is delayed or a stop is overcrowded, agents need to rapidly re-evaluate their action plan. Utilizing a robust spatial indexing system (SID) allows hundreds of agents to query their local grid simultaneously. They can calculate the cost of waiting versus defaulting to walking on the fly. This prevents processing bottlenecks and ensures that the AI feels responsive rather than strictly scripted.

This transit loop serves as a highly scalable micro-test for managing large groups of autonomous units. Because the action scripts are entirely modular, introducing new variables—like traffic congestion, weather impacts, or doctrinal shifts in routine—requires only minor tweaks to the cost evaluation functions. Moving forward, the goal is to integrate these civilian routines into much larger tactical environments, observing the emergent gameplay that arises from simple, need-driven logic.