Pathfinding package

Name: Pathfinding
By AdamO in Discord

Demo game: https://launch.crayta.com/play/zperhsue

The pathfinding package is a collection of scripts and templates that will allow you to have entities in your game which find the shortest routes to a target and move towards it (the target can be any entity, including players).

To use

  • Drag and drop in the Pathfinder template - there should only ever be 1 instance of the pathfinding script in your world
  • Drag and drop in either the PathfindingGridExample or PathfindingNodeExample, then you can extend these to change their functionality, layout or style to suit your game
  • You should be able to use event properties to control the pathfinding to suit your game - for example, a player collision with something might trigger a “guard” robot to set its target to that player and follow them
  • Important: If using the grid example, make sure it is placed at a rotation of 0,0,0 in your world, and that you only build the grid in the positive X and Y directions (i.e. your first grid square can go at 0,0,0 relative to the grid, and the rest should only be positive X and positive Y coordinates). This ensures the gridfinder is able to find your grid squares correctly

The plan is to continue to update and extend this package. Currently it can do the following:

  • Find the shortest path on a given grid from a start position to an end position
  • Find the shortest path given a set of connected nodes from a start node to an end node
  • Move to a target, stop, and reset to the start
  • Continually follow a target
  • Rotate along the path as it moves
  • Calculate where a jump is needed, and perform a jumping action rather than the standard movement

Package components

Pathfinder

  • This is a template which just contains the pathfinding script
  • Your game should have one, and only one, pathfinding script in it (one instance of this template)
  • The pathfinding script takes either grids or sets of nodes, and returns the shortest path through them as a table of nodes
  • This uses the A* pathfinding algorithm - the script is commented to explain how it works, and how the nodes are set up
  • You can use this with the other provided components, or use this by itself in your game, and write the other components yourself, as long as you pass a grid or list of nodes in the right format and use the output correctly

PathfindingGridExample

  • This template is a very basic setup for a grid with a pathfinding object on it
  • The pathfinding entity on this, “PathfindingMouse_(Grid)”, will cycle through movement options when interacted with - move to the target, then stop, then reset position
  • A larger example based on this can be found in the pathfinding demo game, linked above
  • When adjusting this to suit your own game, there are a few things to remember. The parent of this has a “gridControl” script on it, with properties that need to be updated if you change the grid. If you make the grid bigger, you need to update the grid size to reflect this - it should be set to the number of cells in the longest “side” of your grid. If you change the cell sizes, make sure to update the cellSize property, which is in centimetres. Additionally, make sure the grid parent has a rotation of 0,0,0 and build the grid out only in the positive X and Y directions - the grid won’t detect any cells “behind” it, in negative X or negative Y

PathfindingNodeExample

  • This template is a basic setup for a room with a set of nodes, with a pathfinding object on it
  • These nodes are connected together through their properties
  • The room is surrounded by a “PlayerDetectionTrigger” which will queue players who enter the room, and tell the pathfinding entity in the room to follow the player at the front of this queue, or stop moving if there’s nobody in the room
  • The pathfinding entity on this, “PathfindingGhost_(Node)” will move to the nearest node to the player it is following

PathfindingMouse_(Grid)

  • This is the entity used in the pathfindingGridExample template, but it can be used for any pathfinding
  • It is set up to work on a grid, although this can be changed by altering the properties of the pathingFollowerScript

PathfindingGhost_(Node)

  • This is the entity used in the pathfindingNodeExample template, but it can be used for any pathfinding
  • It is set up to work on a set of connected nodes, although this can be changed by altering the properties of the pathingFollowScript

Note: You should be able to use the package with the 2 example templates provided, and adjust them to suit some styles of game. The package can handle a lot of different uses, but therefore can be quite complex to use. Feel free to ask questions in replies to this post, or on Discord in the Lua-Help channel.

11 Likes

Thank you so much for this!! This is just what I needed for my Tower Defense Package :smiley: