- Home /
How do I implement A* pathfinding to my 2d game, without tiles?
Hi there,
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.
Here is what my game looks like: game
How do I use A* to help the enemy (yellow) to go around the walls (black)? My background is X90 by Y75.
Thanks.
your image is not showing in my browser. when you post a question there should be an icon at the top of the box for uploading images to this post.
are you looking to write a script or are you looking to use an asset?
Hi, I can see the hyperlink for it and used the formatting suggested to input a link. I'd be interested in writing a script I think, I'm new to C# and Unity so idk if you could suggest potentially easier to understand things, Thanks :)
Also I should add that the player is controlled with WASD and has very fluid movement which I like and I'm not sure how to tackle the AI moving similarly so without looking blocky/jittery.
I guess I'm also asking about node generation as it is quite a large area. Thanks.
Answer by MajorSmurf · Sep 04, 2018 at 08:47 PM
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.
https://github.com/SebLague/Pathfinding-2D
https://www.youtube.com/watch?v=-L-WgKMFuhE&list=PLFt_AvWsXl0cq5Umv3pMC9SPnKjfp9eGW
So just completed the first tutorial and seems to be going well. Need to try the algortithm bit next. So far this looks the most promising so thank you very much $$anonymous$$ajorSmurf :))
Will set as accepted answer when i finish it :p
Answer by Stratosome · Sep 02, 2018 at 11:29 PM
Hiya,
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:
Ahh didn't think about trying to make it 3D, so the black blocks are cuboids ins$$anonymous$$d of sprites. I'll look into that. Just not sure of how my sprite player model will interact with 3D objects (i.e. can't go through them). Should've been more clear in my question because A* does need tiles/nodes so i guess I meant to ask how to generate nodes too. Thank you :)
Okay so looked into navmesh and because my project is in XY with Z being "up/down" it casts the navmesh on the side of blocks :/
Oh, yeah, I guess that'll happen. You could change your axes, but that's a bit tedious and blah to have to do. Unfortunately, I don't actually have a whole lot of experience with 2D pathfinding and such.
Answer by toddisarockstar · Sep 03, 2018 at 08:10 PM
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[i][];
while(i>0){i--;N[i] = 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;
// put your pathfinding algo here!!!!!!!!
}
if you get stuck somewhere let me know
Answer by telecaster · Sep 04, 2018 at 10:18 PM
I would suggest navmesh. It is solid, easy to use including in 2D with Navmesh surface and can be recalculated at runtime if required using the github navmesh library. https://docs.unity3d.com/Manual/class-NavMeshSurface.html
and
Hey, so I looked briefly at navmesh before and it did seem cool but i had an issue due to my 2D game being on the XY axis and having no z coords. The navmesh would bake onto the "sides" of the walls ins$$anonymous$$d. Got any ideas for that? Thanks