Skip to content
Advertisement

Best solution for Phaser 3 pathfinding? [closed]

I have a game and I want to be able to have enemies pathfind to the player across a tilemap, is there a way to do this without programming my own a* algorithm? I’ve seen a couple supposedly still updated libraries for this, but neither of which have worked, they have both been broken with npm and not worked when added as a script.
The libraries I’ve tried are Easystar and Navmesh.
Any suggestions would be great, and while Phaser support would be useful, pathfinding over a 2D array would still be a good solution for me. Thanks!

Advertisement

Answer

I never tried it myself (until now), but https://github.com/mikewesthad/navmesh is a great plugin and is very extensive.

… A JS plugin for fast pathfinding using navigation meshes, with optional wrappers for the Phaser v2 and Phaser v3 game engines. …

p.s.: on this page you can find a list of available plugins for phaser https://phaserplugins.com/

Update:

After testing in on an simple an example it worked fine, just be careful not to miss any steps.

Here the steps needed:

  1. get the latest plugin file phaser-navmesh-plugin.js (currently from https://github.com/mikewesthad/navmesh/releases/tag/2.1.0)

  2. load it from the html file <script src="phaser-navmesh-plugin.js"></script>

  3. configure the plugin, in the config (there are other ways but this is the more convinient)

    JavaScript
  4. setup the map and the layer which should collide:

    JavaScript
  5. create navMesh, pass the map and the created layer with should have to collisions.

    JavaScript
  6. when needed execute pathfinding (x/y positions are in pixels, not tilesId):

    JavaScript

You can load the plugin in also in the preload function like this

JavaScript

Here the demo code with a mini path trace when done:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement