Introducing Vehicle AI in Godot
After getting the player vehicle up and running, the next natural step was to make other cars drive themselves, that’s where vehicle AI comes in.
At first glance, you might think AI-controlled vehicles are a completely different system, but they actually share a lot in common with the player controller. The main difference lies in who provides the input.
For the player, the input comes from button presses, joystick movement, and camera direction these are all tied to human senses and decision-making. The player looks at the road, feels the car’s response, and reacts.
For AI, there’s no human behind the wheel. Instead, we have to build that “driver” using code including something that can sense the world, make driving decisions, and feed those inputs into the same controller that the player uses.
That’s where three core components come into play:
AIVehicleCharacter
This is the main class that brings the AI vehicle to life. You can think of it as the AI version of PSH: Player Controller but instead of reacting to keyboard or controller inputs, it takes inputs from a virtual “driver.”
The AIVehicleCharacter is structured to use a few key supporting classes or nodes that handle specific parts of the AI logic:
CarController
This class is responsible for actually moving the car, that is, applying engine force, braking, and steering based on the inputs it receives.
It doesn’t care who gives the input (player or AI), it just executes it. This makes it reusable for both player-controlled and AI-controlled cars, which is great for consistency.
We’ll be diving deeper into CarController in the next post since it’s the foundation of how the vehicle behaves physically in the world.
AIDriver
This is the brain of the operation. The AIDriver is responsible for decision-making which is figuring out when to accelerate, how much to steer, and when to brake.
It takes in data from the environment and translates that into driving actions.
You can imagine the AIDriver as the AI’s “hands and feet,” using logic to press virtual pedals and turn the wheel instead of relying on a player’s instinct.
Context
The Context class (or node) acts as the AI’s perception system, its “eyes” and “ears.”
It gathers all the information needed by the AIDriver:
- Position and rotation of the vehicle
- The road ahead (or path)
- Other vehicles or obstacles nearby
- Any special markers or checkpoints
The Context doesn’t make decisions, it just observes. It’s up to the AIDriver to interpret this data and decide what to do next.
Together, these systems form the foundation for AI-controlled vehicles that behave naturally and interact with the same world the player does. The exciting part is that this design keeps things modular, you can swap out the AIDriver logic for a different style of AI (for example, aggressive racers vs. cautious drivers) without touching the rest of the system.
What’s Next
In the next post, we’ll take a closer look at the CarController, this is the shared layer between player and AI vehicles.
We’ll break down how it applies acceleration, braking, and steering in a way that keeps everything physics-driven and consistent across both human and AI drivers.