{"id":13663181,"url":"https://github.com/LostTrainDude/astar-pathfinding-unity","last_synced_at":"2025-04-25T13:32:11.007Z","repository":{"id":37800826,"uuid":"179590949","full_name":"LostTrainDude/astar-pathfinding-unity","owner":"LostTrainDude","description":"A grid-based implementation of the A* Pathfinding algorithm in Unity, that makes use of Priority Queues","archived":false,"fork":false,"pushed_at":"2022-06-16T14:10:01.000Z","size":33,"stargazers_count":29,"open_issues_count":1,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-10T19:34:29.280Z","etag":null,"topics":["astar-csharp","pathfinding","priority-queue","unity"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LostTrainDude.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-04T23:18:52.000Z","updated_at":"2024-05-21T18:23:46.000Z","dependencies_parsed_at":"2022-08-18T08:03:24.940Z","dependency_job_id":null,"html_url":"https://github.com/LostTrainDude/astar-pathfinding-unity","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LostTrainDude%2Fastar-pathfinding-unity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LostTrainDude%2Fastar-pathfinding-unity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LostTrainDude%2Fastar-pathfinding-unity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LostTrainDude%2Fastar-pathfinding-unity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LostTrainDude","download_url":"https://codeload.github.com/LostTrainDude/astar-pathfinding-unity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250825040,"owners_count":21493385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["astar-csharp","pathfinding","priority-queue","unity"],"created_at":"2024-08-02T05:02:20.226Z","updated_at":"2025-04-25T13:32:05.982Z","avatar_url":"https://github.com/LostTrainDude.png","language":"C#","funding_links":[],"categories":["C\\#"],"sub_categories":[],"readme":"# Simple grid-based A* Pathfinding in Unity\n![](https://i.imgur.com/Fr0LxR2.gif)\n\nA simple implementation of the **A\\* Pathfinding algorithm** that makes use of **Priority Queues**, applied to a grid.\n\n## Credits\nThis implementation integrates:\n- [Amit Patel's studies on Pathfinding][tech1]\n- The `SimplePriorityQueue` from [BlueRaja's High Speed PriorityQueue for C#][tech2].\n\nI only imported what I needed for the `SimplePriorityQueue` to work, so please refer to BlueRaja's Git repository for additional options and documentation.\n\n## How to test it like in the GIF above\nIn your Unity Scene:\n- Create a new empty `GameObject`, rename it **Pathfinder** (for ease of use)\n- Attach the `Pathfinder` Component to it\n- Select it in the Hierarchy\n- Start altering the `public` variables available in the Inspector.\n\n## Implementation\n### Introduction\nAs it is, the current implementation considers an 8-direction movement and two kinds of obstacles:\n- Walls - nodes that **cannot** be crossed\n- Forests - nodes that **\"take longer\"** to cross\n\nMovement cost is `1`. However, if the node to be crossed is a Forest node, it costs `2`.\n\n### Create a new GridGraph\nTo initialize a new `GridGraph` that has differently weighted nodes:\n```cs\n// Initialize a new GridGraph of a given width and height\nGridGraph map = new GridGraph(10, 10);\n```\nThis will fill the grid with `Node`s.\n\nNow it's time to initialize the Lists of `Vector2` that will store positions of Walls and Forests on the `GridGraph`.\nOf course you can do this using either `public` Lists of `Vector2` or `private` ones.\n\n```cs\n// Define the List of Vector2 to be considered walls\nmap.Walls = _walls;\n\n// Define the List of Vector2 to be considered forests\nmap.Forests = _forests;\n```\n\n### Find a path\nThen, to define a Start and a Goal you have to define a `Vector2` for each point, so to use it to return a List of `Node`s with the shortest path between them.\n```cs\n// The position of Start and Goal nodes\nVector2 StartNodePosition = new Vector2(0, 0);\nVector2 GoalNodePosition = new Vector2(9, 9);\n\nint x1 = (int)StartNodePosition.x;\nint y1 = (int)StartNodePosition.y;\nint x2 = (int)GoalNodePosition.x;\nint y2 = (int)GoalNodePosition.y;\n\n// Find the path from StartNodePosition to GoalNodePosition\nList\u003cNode\u003e path = AStar.Search(map, map.Grid[x1, y1], map.Grid[x2, y2]);\n```\nParameters `map.Grid[x1, y1]` and `map.Grid[x2, y2]` refer to the `Node`s in the `GridGraph` that are located at `[x1, y1]` and `[x2, y2]`\n\nAgain, you can declare `StartNodePosition` and `GoalNodePosition` to be either `public` or `private`.\n\n## Customization\n### Modify movement costs\nYou can alter this value in the `Cost(Node b)` method contained in the `Graph.cs` script:\n\n```cs\npublic int Cost(Node b)\n{\n    // If Node 'b' is a Forest return 2, otherwise 1\n    if (Forests.Contains(b.Position)) return 2;\n    else return 1;\n}\n```\n\n### Add additional obstacles\nTo include a new obstacle you first have to declare a new `List\u003cVector2\u003e`, at the top of the `Graph.cs` script, for each obstacle you have in mind\n\n```cs\npublic List\u003cNode\u003e Forests;\npublic List\u003cNode\u003e Pits;\n```\n\nThen modify the `Cost(Node b)` method accordingly:\n\n```cs\npublic int Cost(Node b)\n{\n    if (Forests.Contains(b.Position)) return 2; // If Node 'b' is a Forest return 2\n    else if (Pits.Contains(b.Position)) return 3; // If Node 'b' is a Pit return 3\n    else return 1; // Otherwise return 1\n}\n```\n\n   [tech1]: \u003chttps://www.redblobgames.com/pathfinding/a-star/introduction.html\u003e\n   [tech2]: \u003chttps://github.com/BlueRaja/High-Speed-Priority-Queue-for-C-Sharp\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLostTrainDude%2Fastar-pathfinding-unity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLostTrainDude%2Fastar-pathfinding-unity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLostTrainDude%2Fastar-pathfinding-unity/lists"}