Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by magmorta2012 · Sep 02, 2018 at 08:20 PM · c#2daipathfindingenemy ai

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Sep 03, 2018 at 04:40 AM 0
Share

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?

avatar image magmorta2012 toddisarockstar · Sep 03, 2018 at 07:31 PM 0
Share

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 :)

avatar image magmorta2012 · Sep 03, 2018 at 07:36 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image magmorta2012 · Sep 16, 2018 at 10:35 PM 0
Share

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

avatar image
2

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:

  • Nodal Pathfinding in Unity 2D with A* in Non Grid Based Games

  • Pathfinding with 2D, non-grid based movement over uniform terrain

  • Navigation2D - Unity 2D Pathfinding

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image magmorta2012 · Sep 03, 2018 at 07:34 PM 0
Share

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 :)

avatar image magmorta2012 magmorta2012 · Sep 03, 2018 at 08:15 PM 0
Share

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 :/

avatar image Stratosome magmorta2012 · Sep 03, 2018 at 08:41 PM 0
Share

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.

Show more comments
avatar image magmorta2012 · Sep 03, 2018 at 07:53 PM 0
Share

Damn that Navigation2D looks helpful but its $15 :/

avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image magmorta2012 · Sep 04, 2018 at 04:25 PM 0
Share

Thank you very much. I'll look into this soon :)

avatar image
0

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

https://github.com/Unity-Technologies/NavMeshComponents

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image magmorta2012 · Sep 16, 2018 at 06:39 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

241 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get an AI enemy character to stop moving and freeze animation when the FPS camera is looking at it? 1 Answer

Enemy keeps moving in one direction. Help pls! 2 Answers

Looping Between Waypoints 1 Answer

A* pathfinding for 2D top Down 0 Answers

Can't properly duplicate an enemy AI 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges