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.
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.