Nov
16
2014

2D A* Pathfinding (wip 4)

AStar_2D_unity_1

Didn’t find any simple 2D A* for Unity, so started converting/modifying this
(Fast A-Star (2D) Implementation for C# by Christoph Husse) into Unity to use with sprites on a 2D level/map..

Current progress:
– Basic A* grid works (using texture/pixels grid)
– Automatically generate walkable grid area map using raycasts or predefined mask texture
– Make it work with sprites (as movement waypoints/path)
– Needs automatic wall offset (keep distance from walls) : added for raycast version
– Recalculate grid on mouseclick (sets target position)
– Invidual AI bot movement (can set targets per bot)
– AI bot stuck flag (when gets stuck, set bool value)
– Mecanim animations for AI bot
– Invidual Agent controllers
– AI bot reachedTarget bool flag

TODO:
– Option to disable diagonals (especially in low resolution grids)
– Needs automatic wall offset (keep distance from walls) : for texture mask version?
– Diagonal movement is faster than straight?
– Dynamic obstacles (recalculate path if dynamic collider at front? and doors? or just stop moving if enter door trigger area..)
– Chase player + Run away from player
– Path smoothing & smoother movement
– Path optimization? (grid causes jagged movement (going from cell center to another cell center)
– AI Agent flocking or collision avoidance (to that they wont all stack together..)
– AI bot should give up trying to reach target if stuck for a certain time?
– Problem: rigidbody2D.MovePosition() gets sometimes stuck on walls/objects..

Webplayer:
http://unitycoder.com/upload/demos/2DAStarPathFinding/ (V1)
http://unitycoder.com/upload/demos/2DAStarPathFinding2/ (V2)
http://unitycoder.com/upload/demos/2DAStarPathFinding3/ (v3)
http://unitycoder.com/upload/demos/2DAStarPathFinding4/ (v4)

Image#2: Using custom obstacle texture

AStar_2D_unity_2

Image#3: Raycast scanned obstacle map (with adjustable wall avoid radius, thats why some cells are marked red near walls)

AStar_2D_unity_generated_obstacle_map<click to view

Image#4: Sprite following the path (see webplayer v2)
AStar_2D_pathfinding_sprites

 


10 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.