I’ve been creating a game recently and I would like to make my enemies use A* pathfinding to find the player instead of travelling in the player’s direction.
Pathfinding usually uses grids because the whole operation is expensive. The smaller tiles you have, the closer you are to having no tiles or infinitely small tiles, and that starts to become VERY expensive. That doesn’t really mean it’s not possible though. Tiles just make it easy. So if you CAN use tiles, I’d say go with that, but if you’d rather not…
You could potentially look into Unity’s NavMesh stuff. I haven’t used it all that much myself, but it performs a version of pathfinding without you needing to lay down a grid (you do have to define paths, or let it do it for you, but not necessarily be restricted to tiles). Unity’s NavMesh only works for 3D projects, but you could make it look 2D even if it is 3D.
If that doesn’t work, unfortunately all I can really do is point you to some links that might lead you in the right direction:
i seen your pic! anyways writing a pathfinding script is definatly advanced c# programming
to set it up i would recommend an array of arrays to create the two dimmentional data for the pathfinding algo to move through. this way it can be accessed with moving x and y coordiantes in the two indexes. you would also generally use a list to add squares to be checked.
the pathfinding path will be jagged when its done and usually needs a bit path smoothing to look nice. but thats another question and actually simpler than the pathfinding.
here is the first google result i seen:link text
or find an one you like.
if you have extra time its great to learn a bit about A*. you might be banging your head against the keyboard trying to wrapping your brain around the concept for the first time, but if you have the time to spare I think every real developer should understand a*. once you understand the concept you’ll find it can be tailored to do all kinds of cool stuff. like edge detection and flood filling and detecting things within an amount of steps and so on.
i wont write the whole thing for you but here is how you would set things up in c# so you can start working around a grid:
public class node{
//info about each square goes here;
public bool IsWall;
public int steps;
public bool closed;
}
node[][] N;
public void pathfind (){
int i = 20;//height of grid
node[][] N = new node*[];*
_ while(i>0){i–;N = new node[20];}//width of grid_
* //now there is a grid ready to hold our info;*
int x,y;
* //x and y is your start spot …set info in the grid like this* * x = 10; y = 11;* N[x][y].IsWall = true;
So funnily enough I just finished building my own pathfinding and watched a fantastic series of youtube videos by a guy called Sebastian Lague. Now the tutorial are used in a 3D contex but it doesn’t use the Z axis in any real context at all so should be easy to implement into your game (don’t quote me on that). if you don’t want to watch the tutorials, he has a github page where you are freely download the pathfinding. There is also a separate 2D pathfinding on his github just checked while writing this xD.