Get PolyNav on the Unity Asset Store!
PolyNav is a lightweight polygonal based 2D pathfinding solution using A*, that is made to be extremely easy to setup, simple and fun to use!
PolyNav can be great for:
There are three main components in PolyNav that you should be aware of:
The first thing to do is to create your first PolyNav2D map through the Unity top menu “Tools/ParadoxNotion/Create PolyNav Map”. A PolygonCollider2D will automatically be attached as well on that newly created gameobject. By using the default Unity controls for adjusting such a collider, you can define any shape for the navigation polygon that you want. This shape will be the outer boundaries of your map (within which obstacles can exist).
The options on the PolyNav2D component itself are rather few.
To create an obstacle within the master navigation map polygon, you can do so through “Tools/ParadoxNotion/PolyNav/Create PolyNav Obstacle”. Since NavObstacles also come with a PolygonCollider2D, you can edit them the same way as you would the master map polygon. You don’t have to be exact. You can have half an obstacle inside the walkable area, or multiple obstacles overlapping each other if required. The PolyNavObstacle component can of course also be added manually on a gameobject that you want to make an obstacle. This gameobject can pre-exist in the scene or be part of a runtime instantiated prefab if required.
There are a few options in the PolyNavObstalce component:
To make a gameobject pathfind in a PolyNav map, select the gameobject you want and attach the PolyNavAgent component to it.
There are various demo scripts provided to move the agent about for a quick start like for example ClickToMove and PatrolWaypointsRandom. ClickToMove will simply move the agent at the clicked position, while PatrolWaypointsRandom will move the agent
between some preset waypoints at a random selection. Simply attach either on the agent gameobject if you want a quick start!
There are also some other helpful scripts included under the “DEMO/Scripts” folder, worth checking out!
Regarding scripting, there are 2 important methods in the PolyNavAgent:
bool SetDestination(Vector2 goal, Action<bool> callback = null)
Sets a new goal for the agent. You can optionally provide a callback method that will be called on either arrival with a True argument, or if the destination is or becomes invalid with a False argument. The method itself returns whether or not the destination is valid in the first place.
Stop()
Will simply stop the agent from moving.
There are also some important properties that you can use:
Of course, there are a lot more methods and properties in the PolyNavAgent worth checking out!
The PolyNavAgent also has a few events that you can subscribe to and use:
Please check the demo scripts ClickToMove and PatrolWaypointsRandom for example use!
Notice that you can also request a path directly without using the PolyNavAgent component at all, but rather directly from a PolyNav2D map instance and for any purpose. You can do this like so:
PolyNav2D.FindPath( Vector2 start, Vector2 end, Action<Vector2[]> callback)
The callback will provide the resulting path, or null if a path is impossible or not found.
The following image shows a map with a single obstacle within. You will notice that both the master polygon and the obstacle polygon have two different colored lines on their edges, one being Green and another being Grey. When modifying your polygons you need to make sure that the Green line is on the outside while the Grey line on the inside of the navigation area (like shown in the image). If a polygon is not shown as such, you can use the Invert Polygon option (found in both the PolyNav2D and PolyNavObstacle inspectors) to fix the winding of the polygon as to be like in the image below.
Remember that you can enable “Gizmos” in play mode on the top right of the “Game” tab, so that you are able to view the polygon map, obstacles, and agent paths for debugging.
If you have any questions, please contact us or join us on Discord!