- Home /
A* Pathfinding Grid
I want to create a path finding system for my tower defense game. I already tried to create it but it didnt turn out how I wanted it. I understand how A* works I simply want to find a better way to implement it the second time around. The big problems with my last attempt is that I had trouble accessing my grid. What is the best way to store a grid and access it? Second biggest problem was finding a way to search through my grid to find neiboring squares from a given point. What I came up with was really messy and inefficient. any tips you guys can give me would be great.
ps. I dont want to use any A* or path finding tools I want to do as much of this on my own as possible so I can learn more about unity and programming in general.
How did you do it the first time? you don't have to use an array. Another way would be using a Node class, that has a List of 'Node' containing the connecting nodes. Or create an Edge Class that links two Nodes together.
What your saying sounds similar to the fps tutorial path finding on unity is that correct?
So if it's really a grid then a grid of classes representing each cell with data about the height of the land etc is the best way to go. If it's a list of way points then storing the connected nodes is useful in a list. I describe my grid technique in this article: http://unitygems.com/astar-1-journeys-start-single-step/
Answer by Graham-Dunnett · Apr 13, 2013 at 05:03 PM
How about something like this:
// c# example
using UnityEngine;
using System.Collections;
public class grid : MonoBehaviour {
static int _width = 10;
static int _height = 10;
static int[,] _grid;
bool CanMoveNorth(int x, int y) {
if (y == 0) return false; // already on top edge
if (_grid[x,y-1] == 0) return true;
return false;
}
bool CanMoveSouth(int x, int y) {
if (y == _height-1) return false; // already on bottom edge
if (_grid[x,y+1] == 0) return true;
return false;
}
bool CanMoveEast(int x, int y) {
if (x == _width-1) return false; // on right edge
if (_grid[x+1,y] == 0) return true;
return false;
}
bool CanMoveWest(int x, int y) {
if (x == 0) return false; // on left edge
if (_grid[x-1,y] == 0) return true;
return false;
}
// Use this for initialization
void Start () {
_grid = new int[_width, _height];
for (var i = 0; i < _height; i++) {
for (var j = 0; j < _width; j++) {
_grid[j,i] = 0;
}
}
}
// Update is called once per frame
void Update () {
}
}
I'm using a 2-dimensional array in c#. Javascript does not have an easy 2d array. The 2d array stores an int at each location. If the grid has a zero, then the cell can be used as a path. I assume in your game you have blockers at grid locations, so write a non-zero value into the cell. The four functions I have written look to see if you can move north, east, south or west from a given location. There are two tests to make, first, if the cell is already on the edge in which case you cannot move off the grid. Otherwise, look to see if the adjacent cell has a zero or not.
Thats kinda of like what I did last time except I stored my grid in a 2d array that stored a GameObject ins$$anonymous$$d of an int. If I had a character that wanted to find a location though what would be the best way to find were he is on the grid using your method?
Your answer
![](https://koobas.hobune.stream/wayback/20220613100220im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Stuck with A* Pathfinding Script 3 Answers
Making an Array of Tiles from Unity's Grid TileMap feature? 0 Answers
Grid Generation 2 Answers
2D Grid Movement (turn based) 2 Answers
Grid obstacle detection 1 Answer