Greetings all,
The game I am currently developing was in need of a pathfinding script which had a small memory footprint and could work on vast (10,000^2 unit) terrains. This made baking a mesh completely out of the question and since nearly all pathfinding assets require a pre-baked mesh or mesh generated on run-time, I decided to code my own. Now I’d like to share my progress! But do note, this is still a WIP and under heavy development.
My biggest question… is anyone interested in this asset for use in their project?
DEMO NOW AVAILABLE!! (Scroll down to screenshots/demo section)
Free Download from Github: GitHub - mrabenhorst/AdHoc-Pathfinder: An open-source, open world pathfinder for Unity
“Free”? Is it really?
Yes, actually it is truly 100% free. You should still take a look at the license file. There are some requirements like acknowledging that you use it. But it is totally free for personal and commercial use! However, this is powered by donuts and coffee. So donations would be great. Find the donate link on Github if you would like to give back!
What is AHP?
AHP (AdHoc Pathfinder) is a pathfinder based on the A* algorithm and finds a path from source to target during runtime without the need of a pre-baked mesh.
Features:
- Pathfinding without the memory overhead of a baked mesh
- Can be used on terrain of any size without loss of performance
- Multi-terrain compatible (I use and recommend TerrainComposer)
- Automatically detects terrain edges
- Completely accessible via code (C# now, may translate to JS later)
- Stupid simple to operate out of the box
- Outputs raw Vector3 coordinate list rather than forcing you to use it’s own movement system
Planned features: (Which may or may not make it to release)
-
Planetary point-gravity pathfinding compatibility (I can’t afford the asset to test yet. ;.; )
-
Multi-level support for voxel terrains (I can’t afford a voxel asset to test yet. ;.; )
-
Behavior list (Patrolling, fleeing, etc)
-
Better movement behaviors
-
Better heuristics options
Current Goals before 1.0 release:
- Speed improvements (Corutines Added 8/29/14)
- Tag-based In-bounds/Out-of-bounds (Added 8/14/14!!)
- Path refinement (much fewer kinks)
- Refinement of simple movement script
What does AHP have that other pathfinders don’t?
AHP is a niche pathfinder. It is designed for people who:
- Use massive terrains
- Don’t want the memory overhead of a baked mesh
Because AHP generates a grid on the fly, performance is not based on the size of the terrain, rather, it is based on the complexity and length of the desired path. So if your path is 50 units long and it’s on a terrain 100x100, it will perform as fast as if it was on a terrain of 100,000x100,000 but it won’t use any more memory.
How does it work?
It accepts a maximum slope (and soon, “Out Of Bounds” and “In Bounds” tags) to create a path on command that fits the requirements.
Is it the shortest path?
Yes and No. It CAN be. For performance reasons, AHP is not designed to always return the shortest path - rather, a path that fits the requirements. However, different Heuristics will be available so the user can balance performance->accuracy as needed.
A short tour of the components:
AStar Ad Hoc Pathfinder Component:
Description: This is the backbone script of the pathfinder. It’s the only one you need to use if you plan to take the raw Vector3 coordinate list and move your gameobject via your own script.
Start Object: Optional use of assigning a starting point via game object’s transform
Target Object: Optional use of assigning a target point via game object’s transform
Start: Optional use of a Vector3 to assign a starting point (overridden by Start Object)
Target: Optional use of a Vector3 to assign a target point (overridden by Target Object)
Nav Resolution: This is the precision of the navigation grid. Smaller = more precise pathway, longer time to generate a path. This must be smaller than the smallest gap in terrain.
Max Angle: Maximum angle over which a valid path can traverse
Search Timeout: Automatic timeout of a search. This is extremely useful for random location pathfinding where you do not know a path exists at all. If you add a timeout, it will prevent the entire valid area of the terrain (potentially enormous) from being scanned for a point that can never be reached.
Use Optimal Heuristics: A temporary variable which will be changed. Right now only two Heuristics are used: Manhattan Cube (3D version of Manhattan Block) and Distance literal (Vector3.Distance). Checking “true” uses distance literal. More variations will be added.
Raycast Height: 2x maximum terrain height differential
Debug Mode: True = shows debug objects during runtime
Debug Open List Object: Object to show open list locations
Debug Closed List Object: Object to show closed list locations
Debug Path Object: Object to show final path locations
AStar Ad Hoc Simple Movement
Description: This is a VERY simple movement script that uses “MoveTo” to move from one coordinate to the next. It automatically generates a path to the target on command, can be paused, resumed, and stopped on the fly via code.
Speed: Movement speed in units/sec
Is Moving: Publicly exposed variable for whether the object is moving
Debug mode: If true, on start the script will cause the object to move to the Target GO
Target GO: Game object which the script with navigate to. IMPORTANT: The gameobject itself must be disabled.
Screenshots/Demo:
Demo 0.2:
To view the demo, please follow this link (full-screen view is suggested)
Notes about the demo:
- The demo terrain is a 3x3 terrain of 1000 units each (9000sq units total)
- The navigation did not require ANY pre-baked mesh or pre-loading
- The navigation “timeout” is 10 seconds. That means, if the target location is valid but 10 seconds have elapsed since attempting to find a path, it will self-terminate. Otherwise if you have a massive (or infinite) terrain, it may never stop looking.
- The red terrain splat is designed to give visual indication of where the slope >50 degrees. However, it is not 100% accurate because of the spat resolution on the terrain. So if you find you’re navigating over red splat at times, it may very well be just barely valid but the splat doesn’t indicate it.
- AHP currently runs by way of using Corutines. You can control the CPU-usage vs. framerate impact of AHP running on the main thread by altering the cyclesPerFrame (CPF) variable. Larger numbers compute faster but hit framerate performance more. I’ve found that an ideal CPF value is around 20 (which the demo uses). But this can be set at any time - even during runtime.
Finally, the terrain here was randomly generated using TerrainComposer’s Island sample script and textured with RTPv3. The Elk model was made by Jenna Jones by request (she did awesome!) Thanks to all of them!
Screenshots:
Note: These are taken in debug mode.
Red = Closed list
Blue = Open list
Green = Path found
Pathfinding on a 100x100 terrain with obstructions. This took less than a second
Pathfinding on a 4000x4000 terrain set (2x2 of 2000x2000). This took a little more than a second: