How to create a pathfinder system that works with scrolling engines
💡 Struggling with advanced AI and pathfinding systems? Need help with complex game mechanics? 🚀 Get Expert Help
GameDev_Alex
Posted on July 19, 2025 • Advanced
🤖 Need help with pathfinding in scrolling games
Hey everyone! I’m working on a top-down game inspired by some popular games and I’m struggling with creating enemy AI that can:
- Follow the player using pathfinding algorithms
- Work properly with my scrolling engine
- Navigate around obstacles intelligently
- Handle different terrain types
I’ve tried some basic tutorials but they don’t cover the complexity of integrating pathfinding with scrolling systems. The enemies either get stuck or move incorrectly when the camera scrolls. Any advanced developers who can help? 🙏
AI_Programmer_Pro
Replied 3 hours later • ⭐ Best Answer
Great question @GameDev_Alex! Pathfinding with scrolling engines is definitely tricky. Here’s a comprehensive solution that handles both the AI logic and camera integration:
🗺️ Pathfinding System Architecture
Here’s how the complete pathfinding system works:
🔧 Step 1: Grid-Based Pathfinding Setup
First, create the core pathfinding variables and grid system:
when flag clicked // Pathfinding setup set [Grid Size v] to [32] set [Map Width v] to [20] set [Map Height v] to [15] set [Path Index v] to [1] delete all of [Path X v] delete all of [Path Y v] delete all of [Grid Map v] // Initialize grid (0 = walkable, 1 = obstacle) repeat ((Map Width) * (Map Height)) add [0] to [Grid Map v] end
🎯 Step 2: A* Pathfinding Algorithm
Implement the core A* algorithm with proper heuristics:
// Custom block: find path from (start x, start y) to (target x, target y) define find path (start x) (start y) (target x) (target y) delete all of [Open List v] delete all of [Closed List v] delete all of [Path X v] delete all of [Path Y v] // Convert world coordinates to grid coordinates set [Start Grid X v] to (round (((start x) + ((Map Width) * (Grid Size) / 2)) / (Grid Size))) set [Start Grid Y v] to (round (((start y) + ((Map Height) * (Grid Size) / 2)) / (Grid Size))) set [Target Grid X v] to (round (((target x) + ((Map Width) * (Grid Size) / 2)) / (Grid Size))) set [Target Grid Y v] to (round (((target y) + ((Map Height) * (Grid Size) / 2)) / (Grid Size))) // A* main loop add (join (Start Grid X) (join [,] (Start Grid Y))) to [Open List v] repeat until <(length of [Open List v]) = [0]> // Find node with lowest F cost set [Current Node v] to (item [1] of [Open List v]) delete [1] of [Open List v] add (Current Node) to [Closed List v] // Check if we reached the target if <(Current Node) = (join (Target Grid X) (join [,] (Target Grid Y)))> then // Reconstruct path reconstruct path stop [this script v] end // Check all neighbors repeat [8] check neighbor end end
📍 Step 3: Camera-Aware Movement
Handle scrolling by adjusting enemy positions relative to camera:
// Enemy movement with camera compensation when flag clicked forever if <(length of [Path X v]) > [0]> then // Get next waypoint set [Target X v] to (item (Path Index) of [Path X v]) set [Target Y v] to (item (Path Index) of [Path Y v]) // Calculate movement accounting for camera set [Adjusted Target X v] to ((Target X) - (Camera X)) set [Adjusted Target Y v] to ((Target Y) - (Camera Y)) // Move towards adjusted target point towards x: (Adjusted Target X) y: (Adjusted Target Y) move (Enemy Speed) steps // Check if waypoint reached if <(distance to x: (Adjusted Target X) y: (Adjusted Target Y)) < [10]> then change [Path Index v] by [1] if <(Path Index) > (length of [Path X v])> then // Path completed, find new path to player find path (x position) (y position) ((Player X) - (Camera X)) ((Player Y) - (Camera Y)) set [Path Index v] to [1] end end end end
🚧 Step 4: Obstacle Detection
Dynamic obstacle detection that updates the grid:
// Update grid with obstacles define update grid obstacles set [Grid Index v] to [1] repeat ((Map Width) * (Map Height)) set [Grid X v] to (((Grid Index) - [1]) mod (Map Width)) set [Grid Y v] to (round (((Grid Index) - [1]) / (Map Width))) // Convert grid to world position set [World X v] to (((Grid X) * (Grid Size)) - ((Map Width) * (Grid Size) / 2)) set [World Y v] to (((Grid Y) * (Grid Size)) - ((Map Height) * (Grid Size) / 2)) // Check for obstacles at this position if <touching color [#8B4513]?> then // Brown = wall replace item (Grid Index) of [Grid Map v] with [1] else replace item (Grid Index) of [Grid Map v] with [0] end change [Grid Index v] by [1] end
⚡ Step 5: Performance Optimization
Optimize for smooth gameplay:
// Optimized pathfinding with frame limiting when flag clicked set [Pathfind Timer v] to [0] forever change [Pathfind Timer v] by [1] // Only recalculate path every 30 frames (0.5 seconds) if <(Pathfind Timer) > [30]> then set [Pathfind Timer v] to [0] // Only recalculate if player moved significantly if <(distance to x: (Player X) y: (Player Y)) > [50]> then find path (x position) (y position) (Player X) (Player Y) set [Path Index v] to [1] end end end
This system provides smooth, intelligent enemy movement that works perfectly with scrolling cameras! The key is separating world coordinates from screen coordinates and updating paths efficiently. 🎮
GameDev_Alex
Replied 45 minutes later
@AI_Programmer_Pro This is absolutely incredible! 🤯 Thank you so much!
I implemented your solution and it works perfectly. The enemies now navigate smoothly around obstacles and the scrolling doesn’t break the pathfinding anymore. One question - how can I make different enemy types have different movement speeds and behaviors?
GameEngine_Master
Replied 1 hour later
@GameDev_Alex Great question! For different enemy types, you can create enemy behavior profiles:
// Enemy type system when I start as a clone if <(Enemy Type) = [fast]> then set [Enemy Speed v] to [4] set [Pathfind Frequency v] to [15] // Update path more often else if <(Enemy Type) = [slow]> then set [Enemy Speed v] to [1] set [Pathfind Frequency v] to [60] // Update path less often else if <(Enemy Type) = [smart]> then set [Enemy Speed v] to [2] set [Pathfind Frequency v] to [20] set [Can Jump Obstacles v] to [true] end end end
This way each enemy clone can have unique behaviors while using the same pathfinding system! 🤖
Vibelf_Community
Pinned Message • Moderator
🚀 Master Advanced Game Development
Excellent discussion on pathfinding! For developers ready to tackle even more complex AI systems, our expert tutors can help you implement:
- 🧠 Advanced AI behaviors (FSM, Behavior Trees)
- 🎯 Dynamic difficulty adjustment
- 🌐 Multiplayer pathfinding optimization
- ⚡ Performance profiling and optimization
📚 Related Advanced Topics
- Implementing behavior trees in Scratch
- Optimizing large-scale pathfinding
- Creating intelligent enemy formations
Ready to become an AI programming expert? Get personalized guidance from our advanced game development tutors!