{"id":3428,"date":"2014-11-16T02:14:13","date_gmt":"2014-11-15T23:14:13","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=3428"},"modified":"2014-12-05T00:03:40","modified_gmt":"2014-12-04T21:03:40","slug":"2d-a-pathfinding-wip","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2014\/11\/16\/2d-a-pathfinding-wip\/","title":{"rendered":"2D A* Pathfinding (wip 4)"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3429\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/11\/16\/2d-a-pathfinding-wip\/astar_2d_unity_1\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_1.jpg?fit=469%2C380&amp;ssl=1\" data-orig-size=\"469,380\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"AStar_2D_unity_1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_1.jpg?fit=469%2C380&amp;ssl=1\" class=\"alignnone size-full wp-image-3429\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_1.jpg?resize=469%2C380\" alt=\"AStar_2D_unity_1\" width=\"469\" height=\"380\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_1.jpg?w=469&amp;ssl=1 469w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_1.jpg?resize=300%2C243&amp;ssl=1 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/p>\n<p>Didn&#8217;t find any simple 2D A* for Unity, so started converting\/modifying this<br \/>\n(<a title=\"http:\/\/www.codeproject.com\/Articles\/118015\/Fast-A-Star-D-Implementation-for-C\" href=\"http:\/\/www.codeproject.com\/Articles\/118015\/Fast-A-Star-D-Implementation-for-C\" target=\"_blank\">Fast A-Star (2D) Implementation for C#<\/a> by Christoph Husse) into Unity to use with sprites on a 2D level\/map..<\/p>\n<p><strong>Current progress:<\/strong><br \/>\n&#8211; Basic A* grid works (using texture\/pixels grid)<br \/>\n&#8211; Automatically generate walkable grid area map using raycasts or predefined mask texture<br \/>\n&#8211; Make it work with sprites (as movement waypoints\/path)<br \/>\n&#8211; Needs automatic wall offset (keep distance from walls) : added for raycast version<br \/>\n&#8211; Recalculate grid on mouseclick (sets target position)<br \/>\n&#8211; Invidual AI bot movement (can set targets per bot)<br \/>\n&#8211; AI bot stuck flag (when gets stuck, set bool value)<br \/>\n&#8211; Mecanim animations for AI bot<br \/>\n&#8211; Invidual Agent controllers<br \/>\n&#8211; AI bot reachedTarget bool flag<\/p>\n<p><strong>TODO:<\/strong><br \/>\n&#8211; Option to disable diagonals (especially in low resolution grids)<br \/>\n&#8211; Needs automatic wall offset (keep distance from walls) : for texture mask version?<br \/>\n&#8211; Diagonal movement is faster than straight?<br \/>\n&#8211; Dynamic obstacles (recalculate path if dynamic collider at front? and doors? or just stop moving if enter door trigger area..)<br \/>\n&#8211; Chase player + Run away from player<br \/>\n&#8211; Path smoothing &amp; smoother movement<br \/>\n&#8211; Path optimization? (grid causes jagged movement (going from cell center to another cell center)<br \/>\n&#8211; AI Agent flocking or collision avoidance (to that they wont all stack together..)<br \/>\n&#8211; AI bot should give up trying to reach target if stuck for a certain time?<br \/>\n&#8211; Problem: rigidbody2D.MovePosition() gets sometimes stuck on walls\/objects..<\/p>\n<p>&#8212;<\/p>\n<p><strong>Webplayer:<\/strong><br \/>\n<a title=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding\/<\/a> (V1)<br \/>\n<a title=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding2\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding2\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding2\/<\/a> (V2)<br \/>\n<a title=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding3\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding3\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding3\/<\/a> (v3)<br \/>\n<a title=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding4\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding4\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/2DAStarPathFinding4\/<\/a> (v4)<\/p>\n<p>&#8212;<\/p>\n<p><strong>Image#2:<\/strong> Using custom obstacle texture<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3431\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/11\/16\/2d-a-pathfinding-wip\/astar_2d_unity_2\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_2.jpg?fit=576%2C574&amp;ssl=1\" data-orig-size=\"576,574\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"AStar_2D_unity_2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_2.jpg?fit=576%2C574&amp;ssl=1\" class=\"alignnone size-full wp-image-3431\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_2.jpg?resize=576%2C574\" alt=\"AStar_2D_unity_2\" width=\"576\" height=\"574\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_2.jpg?w=576&amp;ssl=1 576w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_2.jpg?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_2.jpg?resize=300%2C298&amp;ssl=1 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/p>\n<p>&#8212;<\/p>\n<p><strong>Image#3:<\/strong> Raycast scanned obstacle map (with adjustable wall avoid radius, thats why some cells are marked red near walls)<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_generated_obstacle_map.jpg\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3440\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/11\/16\/2d-a-pathfinding-wip\/astar_2d_unity_generated_obstacle_map\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_generated_obstacle_map.jpg?fit=960%2C600&amp;ssl=1\" data-orig-size=\"960,600\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"AStar_2D_unity_generated_obstacle_map\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_generated_obstacle_map.jpg?fit=960%2C600&amp;ssl=1\" class=\"alignnone size-medium wp-image-3440\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_generated_obstacle_map-300x187.jpg?resize=300%2C187\" alt=\"AStar_2D_unity_generated_obstacle_map\" width=\"300\" height=\"187\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_generated_obstacle_map.jpg?resize=300%2C187&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_unity_generated_obstacle_map.jpg?w=960&amp;ssl=1 960w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>&lt;click to view<\/p>\n<p>&#8212;<\/p>\n<p><strong>Image#4:<\/strong> Sprite following the path (see webplayer v2)<br \/>\n<a href=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites.jpg\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3443\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2014\/11\/16\/2d-a-pathfinding-wip\/astar_2d_pathfinding_sprites\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites.jpg?fit=960%2C600&amp;ssl=1\" data-orig-size=\"960,600\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"AStar_2D_pathfinding_sprites\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites.jpg?fit=960%2C600&amp;ssl=1\" class=\"alignnone size-medium wp-image-3443\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites-300x187.jpg?resize=300%2C187\" alt=\"AStar_2D_pathfinding_sprites\" width=\"300\" height=\"187\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites.jpg?resize=300%2C187&amp;ssl=1 300w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites.jpg?w=960&amp;ssl=1 960w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Didn&#8217;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: &#8211; Basic A* grid works (using texture\/pixels grid) &#8211; Automatically generate walkable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,3],"tags":[65,678,474,475,679],"class_list":["post-3428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-2d","tag-a","tag-astar","tag-pathfinder","tag-pathfinding"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2014\/11\/AStar_2D_pathfinding_sprites.jpg?fit=960%2C600&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-Ti","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=3428"}],"version-history":[{"count":19,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions"}],"predecessor-version":[{"id":3471,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions\/3471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/3443"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=3428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=3428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=3428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}