BLOCKHEADS - 1 - Movement & Navigation
Download Link - BLOCKHEADS_0.1.0
Overview
Solid movement options and a reliable way to path around the map are core components to believable AI in games. So to kick off this exploration of AI development in Godot 4, I figured this would be the perfect place to start. So I set out to create a foundational movement system that could be shared across whatever AI I develop in the future.
The result is what you see in the video above, where there are 4 unique movement types shown through the different color BLOCKHEADS.
WANDER (BLUE) | PATROL (PURPLE) | FOLLOW (YELLOW ) | FOLLOW (ORANGE) | FLEE (GREEN) |
Waits for a bit, then moves to a random point | Moves between set points around the map | Follows BLUE, tries to maintain a certain distance from it | Follows GREEN, tries to maintain a certain distance from it | Runs away from ORANGE |
Key Resources
Before I move onto explaining some of the methods behind this implementation, I want to take a moment to highlight some key resources that helped me get here.
Jackie Codes - Advance Enemy AI in Godot
This video covers an interest and danger approach to pathing that is by far the best solution I've found to keeping the AI from skimming walls. It was pretty challenging to get this system implemented as this video isn't really a tutorial, but more of an in-depth explanation of the approach. I recommend giving it a watch if you want a better understanding of how the pathing works in this project!
Side Note - I don't think my current implementation is perfectly achieving what's shown here, as my AI will occasionally path awkwardly. I'd love to here any suggestions on how to improve this in the project!
Bitlytic - Finite State Machines in Godot 4 in Under 10 Minutes
I am using the Finite State Machine approach shown here as the foundation to create the unique movement types. Each movement type is a State the AI can use. I currently don't have transitions setup but I plan to expand this system a lot as more features come in.
Development Highlights
For this next section, I want to share some useful code solutions I found during this stretch of development
Navigation
Godot 4's navigation system has been pretty tricky to get right. Agents tend to skim walls, get stuck, and could end up pathing to a point they can't reach if you don't get your setup right. However I found a few tricks to make things a bit easier.
get_final_position()
On the NavigationAgent2D is the get_final_position() method, which returns the final position of the agent's path to reach it's target_position. You can check this final position against the target position to see if your target is reachable, and if not - set your new target to the final. This way you can always set a reachable path for your agents to travel regardless of if your target position is on a navigation area.
Signal - on_velocity_computed()
If you run your CharacterBody2D velocity variable through the on_velocity_computed() signal of the NavigationAgent2D, you can generate a "safe_velocity" that will take into account the avoidance settings of the agent. This is how I have the BLOCKHEADS moving out of the way of one another as they move around the map.
Following
When the AI is following a target, it transitions between a walk and run speed depending on how far away it is from it's target. This creates a pretty believable follow state where the leader influences the speed the follower uses. To achieve this I call the set_follow_speed() function where it checks the current distance from target against the "run_distance", which I've arbitrarily set to be double the navigation agents desired distance to the target.
Fleeing
For running away from a target, I found that running in the opposite direction of the AI felt a bit too rigid and would often force them into walls. To solve this I made AI path towards a perpendicular direction, which creates the circular path shown in the video. I think this feels a bit more intelligent as if this was used in any combat system the AI would natural path out of any direct attacks.
Getting a perpendicular direction is actually pretty simple and I've created two methods here to return the counter and clockwise directions. These methods are based on a Unity implementation I found in a Stack Exchange forum.
Conclusions & Next Steps
Overall I'm pretty happy with what I've achieved here. The movement is pretty dynamic, taking the environment and other AI into account when pathing around the map. There is definitely still room to improve this system, but I feel ready to move onto my next step in the process.
So stay tuned for the next devlog, where I will have added a detection system between the AI and expanded their state machines to get some unique behaviors.
Thanks for reading!
Files
BLOCKHEADS
AI System in development for Godot 4
Status | In development |
Category | Tool |
Author | ByteChompGames |
Genre | Simulation |
Tags | Godot, Pixel Art, Top-Down |
More posts
- 2 - Detection & State MachinesDec 24, 2023
Leave a comment
Log in with itch.io to leave a comment.