Navigating the Stars: AI Movement in ‘Danger Space’

Creating an immersive and engaging gaming experience is the primary objective for any game developer. In my recent 2D space shooter project, I set out to design enemy AI that felt truly integrated with Unity's 2D physics system. I envisioned enemies with a tangible presence in the game world, exhibiting acceleration, momentum, and collision interactions with both the environment and the player. In this post, I'll delve into the AI development process, exploring the pathfinding and movement techniques I employed to bring these physics-driven adversaries to life.

 

Choosing the Right Pathfinding Approach

When setting out to build the AI for this game, I considered various traditional approaches, including node and grid-based pathfinding techniques such as A* and waypoint-based systems. While these approaches are powerful and widely used in game development, they didn't quite fit the bill for what I envisioned for this project. Given the game's emphasis on physics, these techniques would have required significant additional work to achieve the level of physicality and fluidity I wanted in the enemy movement. Furthermore, my game environment includes moving elements (asteroids), which would have added another layer of complexity to these traditional pathfinding methods.


Steering Towards a Solution

The solution came in the form of steering behaviors, a technique that generates movement or acceleration for individual agents based on their surroundings. Steering behaviors gave me the tools to create more lifelike, physics-based movement for my AI agents.

This approach was also more compatible with the dynamic environment of my game. Behaviors such as Pursuit and Evade were particularly useful in achieving the desired enemy AI behavior.

Understanding Steering Behaviors

Steering behaviors were first introduced by Craig Reynolds at GDC 1999. They are techniques to allow autonomous agents to navigate their environment. They calculate a force directing the agent to perform various maneuvers - like seek a target, avoid obstacles, or follow a path - based on its current velocity and the velocity of its target. This enables smoother and more natural movements, with the agent appearing as if it's steering towards its destination.


Evade served as the perfect tool for avoiding predicted collisions. Raycasts projected in front of the enemy ships detect potential collisions, allowing the Evade behavior to steer the ship away from danger.

By using Pursuit, enemies could chase after the player in a believable manner, taking acceleration and velocity into account to predict the player's future position.

In practice, for the navigation of a given agent, multiple steering forces would be accumulated, and that acceleration value would be passed to the Steer method, which would apply the accumulated force to the agent’s Rigidbody2D. For example, the Missile’s player-pursuit logic looks like this:

Github Code Section
gh-repo="BIRD-COMMAND/danger-space-public"
gh-branch="feature/portfolio-polish"
gh-file="Assets/Scripts/Agents/Missile.cs"
gh-start="32" gh-end="41"

Ultimately these combined steering behaviors formed the core of the navigation system, enabling enemies to intelligently traverse their environment.

Previous
Previous

Steering Through Chaos: The Power of Flow Fields in ‘Danger Space’

Next
Next

Object Pooling: An Essential Game Development Practice