A* Pathfinding (E02: node grid)

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ธ.ค. 2014
  • Welcome to the second part in a series on pathfinding in Unity. In this episode we look at how to create the grid of nodes required to begin using the A* search algorithm -
    (explained in detail in part one: • A* Pathfinding (E01: a... )
    Source code: github.com/SebLague/Pathfinding
    If you'd like to support these videos, you can do so with a recurring pledge on Patreon, or a one-time donation through PayPal. / sebastianlague
    www.paypal.me/SebastianLague
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 547

  • @synvian8212
    @synvian8212 8 ปีที่แล้ว +306

    I could listen to this guys voice all day, strangely relaxing . . .

    • @eleos5
      @eleos5 5 ปีที่แล้ว +2

      Not me, for sure!

    • @louissherwood5221
      @louissherwood5221 4 ปีที่แล้ว +1

      he pays hugh grant to do his voice overs xD

    • @peterbengtson7406
      @peterbengtson7406 4 ปีที่แล้ว

      @@louissherwood5221 He has a far posher voice than Hugh Grant, though: he pronounces "off" as "orf" and "gone" as "gorne", for instance, and I suspect he would pronounce "hour" as "ahr" and "flower power" as "flahr pahr" (so-called triphthong smoothing). A very pleasant voice speaking Conservative RP. Not that usual nowadays.

    • @joshual3486
      @joshual3486 3 ปีที่แล้ว

      @@peterbengtson7406 To me it's really not RP, it's more from mainland Europe (pretty sure he's Danish)

    • @gobblestheturkey1413
      @gobblestheturkey1413 3 ปีที่แล้ว +1

      A* asmr

  • @Garbaz
    @Garbaz 9 ปีที่แล้ว +281

    17:00
    (a + b/2) / b = (a/b) + (b/(2*b)) = a/b + 1/2 [ = a/b + 0.5 ]
    And calculating "a/b + 0.5" is easier for the PC than "(a + b/2) / b".

    • @SebastianLague
      @SebastianLague  9 ปีที่แล้ว +138

      Good optimization, thanks for pointing that out.

    • @MrMineHeads.
      @MrMineHeads. 6 ปีที่แล้ว +1

      why the fuck would you do that? this is a float.

    • @scholes8734615
      @scholes8734615 6 ปีที่แล้ว +2

      Not the same fact

    • @brainlow
      @brainlow 6 ปีที่แล้ว +40

      I'd like to add one more thing about this for anyone else who scours the comments for tweaks - three years late, i know, but these tutorials are phenomenal and i've been going through them. I've learned more about A* than I ever thought possible.
      Mathf.RoundToInt((gridSizeX-1) * percentX) is almost right, but not quite.
      By subtracting 1 before multiplying by the percent, you won't go over the number of squares that exist, but you'll be getting a percent of an incorrect number. If you have a grid with 100 or more squares, you may eventually encounter situations where you are off by a square.
      Also, you need to you FloorToInt, because assuming 0 is the left-edge of the grid, it's also the left edge of the first square. 0.5, then, would be the middle of the first square, and so would 0.9, 0.99, and so on. If you ever round up, you can be inaccurate by as much as half a square.
      So instead what i've done is Mathf.FloorToInt(Mathf.Min(gridSizeX * percentX), gridSizeX - 1)

    • @QuestionMark43
      @QuestionMark43 5 ปีที่แล้ว +19

      Did you mean:
      Mathf.FloorToInt(Mathf.Min(gridSizeX * percentX, gridSizeX - 1))

  • @bejoscha
    @bejoscha 4 ปีที่แล้ว +8

    Lovely pace for learning stuff - including mistakes, which are a natural part of the coding experience. I’m glad you are not editing them out.

  • @mr_donutz
    @mr_donutz 4 ปีที่แล้ว +35

    just finished watching this tutorial, now gotta do it 99 more times for me to get it

    • @TroyMakesGames
      @TroyMakesGames 2 ปีที่แล้ว +2

      At least you'll get a skill cape

  • @FranciscoAmorim
    @FranciscoAmorim 7 ปีที่แล้ว +63

    19:30
    if you round down a percentage multiplied by a grid-1 you can end with a target one node away from your actual current node.
    Take x = 14.4 for example:
    (14.4 + 30 / 2) / 30 = 0.98
    x = round(29 * 0.98) = round(28.42) = 28, so you end up at node 28.
    But if your x = 14.4 you are between x=14 and x=15, so you actually are at the far right node (29)
    You should not round and subtract one, you should floor without subtracting:
    x = floor(30*0.98) = floor(29.4) = 29
    edit: you also need to clamp before floor:
    int x = Mathf.FloorToInt(Mathf.Clamp((gridSizeX) * percentX, 0, gridSizeX - 1));
    o/

    • @zoetechandme
      @zoetechandme 3 ปีที่แล้ว +5

      You should also remove these lines>
      percentX = Mathf.Clamp01(percentX);
      percentY = Mathf.Clamp01(percentY);
      Becouse otherwise the bottom Y node is always one above from the current node.
      This is my whole function:
      public Node NodeFromWorldPoint(Vector3 worldPosition)
      {
      float percentX = (worldPosition.x + gridWorldSize.x / 2) / gridWorldSize.x;
      float percentY = (worldPosition.y + gridWorldSize.y / 2) / gridWorldSize.y;
      int x = Mathf.FloorToInt(Mathf.Clamp((gridSizeX) * percentX, 0, gridSizeX - 1));
      int y = Mathf.RoundToInt((gridSizeY) * percentY) +1;
      return grid[x, y];
      }

    • @DancingManson
      @DancingManson 3 ปีที่แล้ว +4

      @@zoetechandme Can you please elaborate this little bit more. Why do you need to add +1 to int y?

    • @umarfaroukabdulmutallab4451
      @umarfaroukabdulmutallab4451 2 ปีที่แล้ว

      o/

    • @OlCowic
      @OlCowic ปีที่แล้ว +1

      This!

    • @camo_man
      @camo_man ปีที่แล้ว +1

      THANK U

  • @Ensiferum888
    @Ensiferum888 9 ปีที่แล้ว

    Amazing tutorial, I'm using Aron Granberg's pathfinding since I couldn't implement it myself. Your explainations are extremely easy to understand. Great work!

  • @naipeadicto
    @naipeadicto 9 ปีที่แล้ว

    Thanks!!!!!!! Incredible explanation! I was struggling setting the grid for my project, but I followed along and it turned out really simple and easy. now to part 3...

  • @PatrickWilliamsphd
    @PatrickWilliamsphd 6 ปีที่แล้ว +2

    I have watched this and Part 1 and am super excited to try this stuff out. I'll be able to quickly create some fun games for my kids with this AI. Keep up the good work friend.

  • @damien5266
    @damien5266 8 ปีที่แล้ว +46

    You're an amazing programmer. Thanks for the videos.

  • @burhanuddin9047
    @burhanuddin9047 3 ปีที่แล้ว

    Thanks a lot. You are the best 😊 teacher. Explained everything in details and your voice have magic, can hear all the day without feel bored. thanks a lot again. 😇😊

  • @MACHINEBUILDER
    @MACHINEBUILDER 3 ปีที่แล้ว +3

    I saw this video in my recommended, and I've already watched it twice, but I clicked on it again just to listen to your voice lol

  • @MrSquishedsquashed
    @MrSquishedsquashed 9 ปีที่แล้ว

    Really great tutorial!
    Quick, to the point and clear.
    Thanks!

  • @10chaos1
    @10chaos1 9 ปีที่แล้ว +1

    thank you very much for this Sebastian i now understand what i need to do to make a placement grid for my citybuilder game project

  • @bwegrzyn
    @bwegrzyn 3 ปีที่แล้ว +1

    I'm trying to implement this in Java and it works fine. I only had problem with percentX and percentY, wich were returning 5X bigger value than they should, but I fixed it. Thanks for great tutorial!

  • @web_tutorials_for_beginners
    @web_tutorials_for_beginners 9 ปีที่แล้ว +2

    Sebastian Lague More, and more videos please, you are a great tutor!!!
    More videos about anything! In every video I learn something new.

  • @jessejones6756
    @jessejones6756 4 ปีที่แล้ว +16

    No idea what you're talking about but I'm still interested.

  • @AA-vf4fv
    @AA-vf4fv 7 ปีที่แล้ว

    Thanks for the great Tutorial. It helped me very much.
    Also good explanation and easy to follow.

  • @Undead2k
    @Undead2k 6 ปีที่แล้ว +2

    Thanks for making this tutorial and for also adding the source files for the 2D project somewhere in the comments (as the video is for 3D). I am still learning c# and unity and now trying to learn A* pathfinding. I am finding this really useful and educational.

    • @dead2gamer
      @dead2gamer ปีที่แล้ว

      Hey can you link me up with the code for 2D. I'm working with this and trying to figure out how to check for collisions on 2D

  • @JakeDownsWuzHere
    @JakeDownsWuzHere 8 ปีที่แล้ว +1

    worked perfectly for me. really enjoying this series. :D thanks for sharing it

  • @lamdasoftworks1165
    @lamdasoftworks1165 6 ปีที่แล้ว +11

    I'd love to see an update on this, using unity's new Tilemap Grid system.

  • @tumankito7776
    @tumankito7776 3 ปีที่แล้ว

    I am a big fan of you! Great way to explain the logic of your code, thanx for the amazing tutorial.

  • @MrNintendeion
    @MrNintendeion 3 ปีที่แล้ว

    Omg I know I'm a bit late so you may well not see this comment but this tutorial dude wow. I'm trying to implement A* into an Xcom style strategy game and after hours of trying to follow another TH-camr (I won't name names but god it was infuriating), this tutorial is just what I needed :) I've followed this all the way through and feel so much better about A* now, you're a great programmer mate thanks so much.

  • @dennischan2263
    @dennischan2263 6 ปีที่แล้ว

    excellent tutorial, thank you very much dude, you save my FYP and my life

  • @tshichan
    @tshichan 4 ปีที่แล้ว +2

    I wish the youtube has another LIKE button !! thank you Sebastian for this piece of magic code :)

  • @gadgetboyplaysmc
    @gadgetboyplaysmc 4 ปีที่แล้ว +3

    There's another way to do 17:17 on the NodeFromWorldPoint function.
    Which is to just Vector3.InverseLerp(-gridPosition*.5,gridPosition*.5,worldPosition.x)
    It's automatically clamped to 0 and 1 so no need to do Mathf.Clamp or whatever:
    Mathf.InverseLerp(-gridWorldSize.x*.5f,gridWorldSize.x*.5f, worldPosition.x);
    Mathf.InverseLerp(-gridWorldSize.z*.5f,gridWorldSize.y*.5f, worldPosition.z);
    Remember, gridWorldSize and worldPosition are 1:1 proportion.
    Little definition of Inverse Lerp:
    InverseLerp(min,max,t)
    What InverseLerp does is that it returns a float value that converts value t into a value between 0,1.
    Kinda like Lerp except t is a value you pass in that is between 0 and 1 and returns a float value that is between min and max.

  • @jasperh6618
    @jasperh6618 9 ปีที่แล้ว +1

    I absolutely love this ^^ such a clear explanation :)

  • @moduntilitbreaks
    @moduntilitbreaks 4 ปีที่แล้ว +3

    This guy is the reason i requested .1x playing speed from TH-cam.

  • @pixeldust8362
    @pixeldust8362 4 ปีที่แล้ว +2

    I came here from this guy named Daniel... he kind of just reuploaded your tutorial with his following your tutorial... he did NOT fix the "that's y it's meant to be z" for the percent value. I've been stuck for a week. Lol.

  • @DriitzzCabal
    @DriitzzCabal 6 ปีที่แล้ว

    I would like to see how you would go about having an Enemy AI that jumps up and down platform to chase the player , in a 2D SideScroller platformer, Which route would you pick ? A* pathfinder , Raycast , Grid base ,MeshBase. Possibly also giving your opinion on difference between them with Pros and Cons.

  • @yuuryu2394
    @yuuryu2394 7 ปีที่แล้ว +1

    exactly what i was looking for, thanks a lot! :D

  • @MrDavidCollins
    @MrDavidCollins 3 ปีที่แล้ว

    Just implemented this for my infinite world map! I love GMS2's new class/object programming

    • @kevinmolinari8953
      @kevinmolinari8953 7 หลายเดือนก่อน

      what?... how? seriously, im lost how do u do it for an infinite map

  • @michaelveloso
    @michaelveloso 8 ปีที่แล้ว +1

    good clear instructions. thanks

  • @FeuerfallFan
    @FeuerfallFan 7 ปีที่แล้ว +2

    if you have an inaccurate position from the NodeToWorldPoint, make sure your GameObjects position with the Grid Script is set to Vector3(0,0,0)

  • @BlockerofDoom
    @BlockerofDoom 7 ปีที่แล้ว +2

    Awesome video, one problem that I encountered while testing this code is that when you pass the player position parameter to NodeFromWorldPoint you have to convert coordinates to local relative to grid object with Transform.InverseTransformPoint otherwise if your grid is not centered on 0,0,0 then it will not work :)

    • @Tevan1n
      @Tevan1n 3 ปีที่แล้ว +1

      Hey there, I know it's 3 years late, but how would you go about doing this? Could you clarify? Thanks.

  • @MangaPianet
    @MangaPianet 2 ปีที่แล้ว

    Talk about A(*) programmers! Great video and great code, I love all your videos!

  • @TheCJBaldwin
    @TheCJBaldwin 8 ปีที่แล้ว

    Great tutorial - many thanks

  • @Bluzora.
    @Bluzora. 9 ปีที่แล้ว

    Useful! Love it! Keep it going man! :D

  • @worempie
    @worempie 7 ปีที่แล้ว

    A little late and don't know if this has been posted already or not, but currently you don't use the unwalkableMask. If you have for example other gameobjects with a collider, they will also be added to the non-walkable grid. To fix this you have to add the layer mask to line 28:
    bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask));
    Also to update the grid just run the CreatGrid(), for example by pressing the N button:
    if (Input.GetKeyDown(KeyCode.N))
    {
    CreateGrid();
    }
    or from a different script (don't forget to make CreateGrid() public:
    if (Input.GetKeyDown(KeyCode.N))
    {
    GameObject.Find("A*").GetComponent().CreateGrid();
    }

  • @sits
    @sits 8 ปีที่แล้ว +49

    We should also take node radius and our A* object position into account when getting node from world point or the results will be pretty inaccurate.
    Here's what I ended up with:
    float percentX = (worldPosition.x - transform.position.x) / gridWorldSize.x + 0.5f - (nodeRadius / gridWorldSize.x);
    float percentY = (worldPosition.z - transform.position.z) / gridWorldSize.y + 0.5f - (nodeRadius / gridWorldSize.y);

    • @ukaszw7924
      @ukaszw7924 8 ปีที่แล้ว +1

      +Alexander Sorokin Thanks!

    • @ashleytwo
      @ashleytwo 8 ปีที่แล้ว

      +Alexander Sorokin I think I'm having this problem. When I was following along and testing and put it at 0,0,0 it was all fine. Now that I actually want to implement it in my game as soon as it pathfinds it's trying to go somewhere weird.
      I think its the NodeFromWorldPoint method that's doing it but can't figure out how to fix it.
      Your reply seems to suggest there should be some code. Mind posting it again as I can't see anything and I'm hoping you have the answer!
      Thanks :)

    • @sits
      @sits 8 ปีที่แล้ว

      Ashley Jones dunno why you don't see it, I am still seeing it in my reply.
      Anyway, here you go:
      gist.github.com/alsorokin/2550976f89b5649314a4

    • @FeuerfallFan
      @FeuerfallFan 7 ปีที่แล้ว +1

      Helped me, thank you very much! :)

    • @TheTwyker
      @TheTwyker 7 ปีที่แล้ว +1

      this is the most important comment on youtube I've ever read! UP YOU GO, SIR!
      WITHOUT THIS, YOU CAN'T USE IT IN YOUR GAME, meaning: you can't move the grid.

  • @yagohenriquepereira3367
    @yagohenriquepereira3367 8 ปีที่แล้ว

    amazing tutorial! Thanks

  • @RetroEcoChicken
    @RetroEcoChicken 4 ปีที่แล้ว

    you can use this with his cavesystem if you diable the node script then enable it after runtime when the mesh and collision has been generated.

  • @sallycleverdoggo700
    @sallycleverdoggo700 2 ปีที่แล้ว

    This is so good! Thank you!

  • @pablokalemba2924
    @pablokalemba2924 5 ปีที่แล้ว

    Thanks !! you saved my proyect

  • @zhaoxuefei3829
    @zhaoxuefei3829 2 ปีที่แล้ว

    omg, what a amazing video for a star, thank you very much

  • @badrballish9328
    @badrballish9328 3 ปีที่แล้ว

    Great tutorial thank you very much.

  • @Robsfund
    @Robsfund 8 ปีที่แล้ว +1

    Hey, great video! Quick question, if I want a grid node for a 3d terrain, should I just raycast down and create each node at the hit point, is it that simple?

  • @javiermurarios3960
    @javiermurarios3960 8 ปีที่แล้ว

    very wonderfull tutorials man, im enjoying so much your tutorials. And i want to add something very little, i dont like that NodeFromWorldPoint get the node to the right or bottom of the capsule so i modified the percent so they are calculated from the center of the capsule, but i dont know if the modification i did its really working on any case, here is my modification:
    percentX = (worldPosition.x + gridWorldSize.x/2 + nodeRadius) / gridWorldSize.x;
    percentY = (worldPosition.z + gridWorldSize.x/2 + nodeRadius)/ gridWorldSize.y;
    pd: forget it, it doesnt work, i was testing it just for the top right, the other sides doesnt work.
    pd2: well i think now works, just to make more precise the transition between nodes:
    public Node NodeFromWorldPoint(Vector3 worldPosition) {
    float percentX = (worldPosition.x + gridWorldSize.x/2) / gridWorldSize.x;
    float percentY = (worldPosition.z + gridWorldSize.y/2) / gridWorldSize.y;
    percentX = Mathf.Clamp01(percentX);
    percentY = Mathf.Clamp01(percentY);
    int x = Mathf.RoundToInt((gridSizeX-1) * percentX);
    int y = Mathf.RoundToInt((gridSizeY-1) * percentY);
    //here start my added modification
    if (grid [x, y].worldPosition.x + nodeRadius < worldPosition.x && x < gridSizeX - 1 && x > 0)
    x++;
    else
    if (grid [x, y].worldPosition.x - nodeRadius > worldPosition.x && x < gridSizeX - 1 && x > 0)
    x--;
    if (grid [x, y].worldPosition.z + nodeRadius < worldPosition.z && y < gridSizeY - 1 && y > 0)
    y++;
    else
    if(grid [x, y].worldPosition.z - nodeRadius > worldPosition.z && y < gridSizeY - 1 && y > 0)
    y--;
    return grid[x,y];
    }
    this should be easy for you but since i'm new with unity i wanted to see if im doing it right.

  • @L0932
    @L0932 9 ปีที่แล้ว

    Sebastian Lague
    If I wanted to make the cyan colored detection cube for the player to work properly, does the plane have to be centered at 0,0,0? I've tested this out and that seems to be the case. What's a good solution to have the player detection working properly for planes positioned at any world position? Thanks in advance.

  • @jdelaay025
    @jdelaay025 8 ปีที่แล้ว +1

    fantastic tutorial Sebastian!! I have one issue. The playerNode will not update or show as a different color. I even continued on I even continued on to the next tutorial for pathfinding to see if the old code would correct itself and it did not. I changed the zed and y coordinates and everything but it would work on either the x or y plane. So that means no path finding. It does work for the layer mask but none of the update type of functionality. Just the stuff that's on the startup section
    what do you suggest

  • @klevialushi571
    @klevialushi571 2 ปีที่แล้ว +1

    its an old video but gotta love the fact that you got stockfish there

  • @StarkTech47
    @StarkTech47 6 ปีที่แล้ว

    Hi very good tutorial and very interesting.
    When I was whatching this I asked me if is it possible to adatpe this path finding to a shpere ?
    If it is not, do you have any idea to help me ?

  • @romainchow5706
    @romainchow5706 6 ปีที่แล้ว +2

    what does the Node[,] variable do in Grid? It's not working for me, do I have to attach the node script?

  • @Krebzonide
    @Krebzonide 6 ปีที่แล้ว +2

    How would you change this to make it work with 2d sprites instead of cubes? So far my best idea is to hide cubes behind the sprites which has worked but seems like it might cause lag with lots of obstacles.
    Edit: Looking over it it seems that it is the boxes collision that causes the detection so I just need some way to add collision to sprites.

  • @aidansoles5113
    @aidansoles5113 9 ปีที่แล้ว

    You are a boss at debugging....

  • @MoloSolo
    @MoloSolo 8 ปีที่แล้ว

    Congratulations, I'm really enjoying your tutorials!
    Just one question. Don't you think it could be better to define variables "worldPoint" and "walkable" (lines 27 and 28) outside of the "for" instructions and assign the new value inside?
    I think it's faster to define these variables just one time instead of creating and destructing them in each loop.
    What do you think?

  • @canfly8385
    @canfly8385 6 ปีที่แล้ว

    thanks man, youre great.

  • @theearthisdonut-shaped2142
    @theearthisdonut-shaped2142 9 ปีที่แล้ว

    How did you configure the lighting and skybox like that?

  • @RoutineJustice
    @RoutineJustice 9 ปีที่แล้ว +4

    I was JUST wondering how I was going to do the AI for the top down shooter. I've had some exposure to A* before but never in the context of actually doing it.
    Sebastian: I love you.

    • @windowsxseven
      @windowsxseven 3 ปีที่แล้ว +1

      lmao gay

    • @Phagocytosis
      @Phagocytosis 2 ปีที่แล้ว

      ​@@windowsxseven I love you

    • @windowsxseven
      @windowsxseven 2 ปีที่แล้ว +1

      @@Phagocytosis 💝💝💘💋💋💌💓💕💞❤️‍🔥💟💚💙💜😘😘😙🥰💕💗💖

  • @HendrioLuis
    @HendrioLuis 9 ปีที่แล้ว

    Great tutorial :)

  • @Random0scar
    @Random0scar 6 ปีที่แล้ว

    Does the ondrawgizmos only update once? Because I have a game where you will be able to add and remove blocks and they dont turn red/white if I do.

  • @marielauredebeaune1780
    @marielauredebeaune1780 ปีที่แล้ว

    thanks for this video how can I do for only make a line renderer betwen a few cubes in taking the shortest path?

  • @josepacio7579
    @josepacio7579 8 ปีที่แล้ว

    I've been trying to figure this out for a while now, but how could I modify this to make each node a bit more rectangular, as in increase the width of each node?

  • @alisontaylor4589
    @alisontaylor4589 8 ปีที่แล้ว +1

    is it possible to use this in a game in unity where your scene is so smalll that your node points to world points need to get small floats/doubles? I'm trying to use this in a game but sometimes my enemies go to spots they shouldn't be able to and get stuck, i think its something with the conversion but not sure.

  • @trinketos
    @trinketos 9 ปีที่แล้ว +3

    OverflowException: Number overflow.
    Grid.CreateGrid () (at Assets/Pathfinding/Pathfinding_test/Grid.cs:22)
    Grid.Start () (at Assets/Pathfinding/Pathfinding_test/Grid.cs:18)
    I use Unity 5

    • @shezfroze7137
      @shezfroze7137 9 ปีที่แล้ว

      trinketos I've got same error (grid = new Node[gridSizeX, gridSizeY];) of Number overflow. have you got the solution? I am also using unity 5.0.0.

    • @trinketos
      @trinketos 9 ปีที่แล้ว

      no, that's bad for me xD, maybe I have to ask to some "experts" in unity xD

    • @shezfroze7137
      @shezfroze7137 9 ปีที่แล้ว

      trinketos Ooo :|

    • @trinketos
      @trinketos 9 ปีที่แล้ว

      :D

    • @Mrpngu
      @Mrpngu 8 ปีที่แล้ว

      +trinketos You managed to get this part working?

  • @sandichhuu
    @sandichhuu 7 ปีที่แล้ว

    Great videos

  • @catanator123654
    @catanator123654 7 ปีที่แล้ว +48

    So I got all the code right but it just wouldn't work and after a half hour of checking over the code turns out I misspelled "position" 😑

    • @Villfuk02
      @Villfuk02 7 ปีที่แล้ว +18

      that's 40% of being programmer - fixing simple mistakes ¯\_(ツ)_/¯

    • @elektro36
      @elektro36 5 ปีที่แล้ว +7

      A CLASSIC

    • @chappie3642
      @chappie3642 4 ปีที่แล้ว +7

      How did you not see that on the error log lmao

    • @alexanderrupp1209
      @alexanderrupp1209 3 ปีที่แล้ว +1

      That’s a mood

    • @daksheshgupta7045
      @daksheshgupta7045 3 ปีที่แล้ว

      get a linter

  • @PauloHenrique-em2ly
    @PauloHenrique-em2ly 3 ปีที่แล้ว

    Just a question, if i make the gridWorldSize with diferent values, like (x=40, 15) the variable WorldBottomLeft still get the correct value?

  • @leddy_
    @leddy_ 9 ปีที่แล้ว

    I've been using "Sublime Text 2" in Unity for a little while now, mostly because I love its Ctrl+D function. When you mentioned the replace x with y using Cmd+R at 8:32, is that a plugin? I do notice you are using iOS, while I am on Windows, but I tried using Ctrl, the Windows button, Alt, Shift, and then went on Google to try and look it up. I can't find anything on it. This might be a stupid question with an obvious answer, but I'm just very confused right now and would love to know how you did that. :P

  • @riteshrobotics9713
    @riteshrobotics9713 2 ปีที่แล้ว

    A* Life Saver
    Was a really good video!

  • @3personal5me8
    @3personal5me8 2 ปีที่แล้ว

    Me, about to go to bed because I work tomorrow: "Well, guess I'm watching this whole series first".

  • @JFkingW
    @JFkingW 9 ปีที่แล้ว

    Good Tutorial! Do you know a way how to detect touch gestures?

  • @spectreshadow9901
    @spectreshadow9901 8 ปีที่แล้ว

    hi Sebastian, i like your videos and thank you for uploading and sharing them, i am having problems with the grid script. i am writing the code as you show but i keep getting build errors and am stuck on video 2 placing the script on to A* empty. please can you help? i use windows 7 as i cannot afford a mac. thank you in advance

  • @itsybitsypixel
    @itsybitsypixel 7 ปีที่แล้ว +6

    *ANYONE WHO IS HAVING INACCURATE POSITIONS FROM THE NodeFromWorldPoint READ THIS*
    I found that the "NodeFromWorldPoint" method returned an inaccurate position while on the sides of the grid, so I tried remaking it too show a more accurate position. It's basically the same but it skips the percent and uses the nodeRadius and nodeDiameter variables and that somehow works.
    int x = Mathf.RoundToInt((worldPosition.x + gridWorldSize.x / 2 - nodeRadius)/nodeDiameter);
    int y = Mathf.RoundToInt((worldPosition.y + gridWorldSize.y / 2 - nodeRadius)/nodeDiameter);
    x = Mathf.Clamp(x, 0, gridSizeX - 1);
    y = Mathf.Clamp(y, 0, gridSizeY - 1);
    return grid[x, y];
    ^This code is inside the method^
    I tested this and it worked and gave me a more accurate position, but I have not tested this with the rest of the tutorial so be warned.
    Also I am using this for a 2D game so I used the Y position instead of the Z position in "worldPosition". If you are making a 3D game use Z instead.

    • @Seanld98
      @Seanld98 5 ปีที่แล้ว

      I know this is old, but this tutorial is probably the best for pathfinding, and sorry for asking a question 2 years later, but I am also doing a 2D game, and for me when I first test to see the red and white squares, they are all white for me. None of them turn red. I changed the code so it showed in an XY way and removed the Z, but it doesn't work. I think it has something to do with bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask)); The 3D physics I think is the problem, but not sure how to fix it. Ideas?

    • @smilos99
      @smilos99 5 ปีที่แล้ว +1

      @@Seanld98 Have you created a layer mask for unwalkable objects on your scene? If you have check if you have added the layer mask to your unwalkable game objects. Also if that is not the case check if you maybe forgot to change the "Unwalkable Mask" parameter on your "Grid" script from "Default" to "Unwalkable". If this is all correct for you maybe you miss-spelled something while writing the gizmos? This "bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask)); " seams fine to me :P.
      Hope this helps.

    • @Seanld98
      @Seanld98 5 ปีที่แล้ว

      @@smilos99 I have double checked all that, but what I found changed it was adding the normal Box Colliders (non 2D) to the 2D boxes and that seemed to work so I'm not sure why it wouldn't work in the 2D space, thanks though :)

    • @MaskedImposter
      @MaskedImposter 5 ปีที่แล้ว +2

      @@Seanld98 I'm currently going through this tutorial. This worked for me: bool walkable = !(Physics2D.CircleCast(worldPoint, nodeRadius, Vector3.forward, nodeRadius, unwalkableMask)); 2D and 3D colliders don't collide, so you gotta go 2D!

    • @darkopeninger1915
      @darkopeninger1915 5 ปีที่แล้ว

      Thanks
      PixelMannen! I had the same issue and your comment solved it.

  • @naderabdullah8569
    @naderabdullah8569 8 ปีที่แล้ว

    How do you make those words highlighted in white in Solarized Dark?

  • @davidpike5959
    @davidpike5959 6 ปีที่แล้ว +86

    wow, copying this guy makes me feel clever :O

  • @azrailnone
    @azrailnone 3 ปีที่แล้ว +1

    Any suggestions for me to find the best way to learn C# like that??? have been struggling for a while now, quite a complex lang.

  • @alexter-sarkisov8321
    @alexter-sarkisov8321 7 ปีที่แล้ว +2

    I changed the code a bit to account for a fact that a large object (e.g. player ship) can occupy several nodes. To create gizmos with the same color for all such nodes, I added a boolean to Node object (playerhere) and a condition in the CreateGrid() method ("player_shield_weak" is simply a collider object of the player ship):
    if (walkable == false) {
    Collider[] col = Physics.OverlapSphere (worldPoint, nodeRadius);
    if (col [0].gameObject.tag == "player_shield_weak") {
    grid [x, y] = new Node (walkable, worldPoint, true);
    } else {
    grid [x, y] = new Node (walkable, worldPoint, false);
    }
    } else {
    grid [x, y] = new Node(walkable, worldPoint, false);
    }
    This creates a Node object with playerhere =true, and then, in DrawGizmos() method I just changed the condition to check this boolean:
    if (node.playerhere == true)
    {
    Gizmos.color = Color.cyan;
    }
    and it worked!

  • @levonravel3698
    @levonravel3698 2 ปีที่แล้ว +11

    Be aware if you are following this tutorial there is a pitfall, the map is suppose to be at 0,0,0 it cant move if you do the path to the character is off. To fix this do this instead of what was provided.
    private Node GetNodeFromWorldPoint(Vector3 worldPosition)
    {
    var percentX = (worldPosition.x - transform.position.x + GridSize.x / 2) / GridSize.x;
    var percentY = (worldPosition.y - transform.position.y + GridSize.y / 2) / GridSize.y;
    Thank me later but this portion really threw me off and solved it in about 5 minutes.

    • @impact3373
      @impact3373 ปีที่แล้ว +1

      you literally saved my life thanks

    • @juicedup14
      @juicedup14 ปีที่แล้ว +1

      This needs to get pinned

    • @diggerpy
      @diggerpy ปีที่แล้ว +1

      thanks

    • @dumitrascumihai-cosmin1615
      @dumitrascumihai-cosmin1615 ปีที่แล้ว +1

      It is like this actually:
      float percentX = (worldPosition.x - transform.position.x + gridWorldSize.x/2) / gridWorldSize.x;
      float percentY = (worldPosition.z - transform.position.z + gridWorldSize.y/2) / gridWorldSize.y;
      But, thank you for saving my day!

    • @sanmaro91
      @sanmaro91 10 หลายเดือนก่อน +1

      thank you you are great dad

  • @sergiopradabarrios2243
    @sergiopradabarrios2243 8 ปีที่แล้ว

    Thank you

  • @pedicagames3729
    @pedicagames3729 6 ปีที่แล้ว

    I usually use this for "index from positions":
    int x = (int)((worldPosition.x - worldBottomLeft.x) / (nodeRadius * 2));
    int y = (int)((worldPosition.z - worldBottomLeft.z) / (nodeRadius * 2));
    x = Mathf.Clamp(x, 0, gridSizeX - 1);
    y = Mathf.Clamp(y, 0, gridSizeY - 1);
    return grid[x, y];
    ps: worldBottomLeft must be global and calculate at Start()

  • @DerXavia
    @DerXavia 7 ปีที่แล้ว

    I wonder why you use the sphere check instead of a boxcheck, isn't that more efficient?

  • @wakka2100
    @wakka2100 7 ปีที่แล้ว

    i follow your tutorial i got an error at gizmos says null reference and the grid only draw cube to right not all the places any way to fix this ?

  • @sachacerf6513
    @sachacerf6513 7 ปีที่แล้ว

    Hi sebastian. Ive got a problem with the playerNode : its not at the player position, but it moves correctly when im moving the player around. I think its a problem with the NodeFromPosition method, but i dont see any problems. Any idea?
    Edit : Ok, the Astar GameObject had to be at (0,0,0)

  • @tr25ny
    @tr25ny 7 ปีที่แล้ว

    trying to get the world bottom left based on the z rotation. so that if my grid object is rotated on a 45 degree angle the world bottom left will be aligned diagonally is this possible. (I'm using it in a 2d space)

  • @MrCheliab
    @MrCheliab 8 ปีที่แล้ว +5

    Hello Sebastian.
    I have problem with your code in function NodeFromWorldPoint(). They get incorrect coodinates of node.
    This is my solution, if sobody have same problem:
    public Node NodeFromWorldPoint(Vector3 worldPosition)
    {
    // Move coordinates in right position
    float linearPosX = worldPosition.x - worldBottomLeft.x;
    float linearPosY = worldPosition.y - worldBottomLeft.y;
    // Get float value of positions in new coordinates
    float floatPosX = (linearPosX / nodeDiameter) - nodeRadius;
    float floatPosY = (linearPosY / nodeDiameter) - nodeRadius;
    int x = Mathf.RoundToInt(floatPosX);
    int y = Mathf.RoundToInt(floatPosY);
    x = Mathf.Clamp(x, 0, gridSizeX - 1);
    y = Mathf.Clamp(y, 0, gridSizeY - 1);
    return grid[x, y];
    }

  • @videomasters2468
    @videomasters2468 9 ปีที่แล้ว +7

    if possible i would really like to see a tutorial/tutorial series on procedural level generation :)

    • @SebastianLague
      @SebastianLague  9 ปีที่แล้ว +10

      Yeah that'd be fun :) Do you have something particular in mind? I could easily do some sort of cool cave generation using using cellular automata..

    • @videomasters2468
      @videomasters2468 9 ปีที่แล้ว +1

      Sebastian Lague
      Yeah, either your suggested cave generation or some kind of race track, which would be of greater interest to you ?

    • @JoshD22
      @JoshD22 9 ปีที่แล้ว

      Another great tutorial Sebastian! Procedural generation sounds like a great idea! I would also like to suggest procedural voxel terrain in Unity, maybe block type worlds like Minecraft or smooth voxel terrains by using something like marching cubes or even better Dual Contouring, which allows you to have sharp features for buiulding but the smoothness that you want for the ground.

    • @trevor7066
      @trevor7066 9 ปีที่แล้ว +1

      Sebastian Lague
      I think something along the lines of Terraria style map generation would be extremely useful.

    • @BrainRobo
      @BrainRobo 9 ปีที่แล้ว

      Sebastian Lague
      Also, if possible random planet generation, using spheres as the planet for space exploration, would be very intriguing.

  • @marypearldegamo6371
    @marypearldegamo6371 6 ปีที่แล้ว

    hey, how can i extend the nodes to another floor ,i want to find path from 1st floor to 2nd floor?

  • @alexandergilbert7572
    @alexandergilbert7572 9 ปีที่แล้ว

    It might be a fun little project to get A* path finding to work on your spherical world.

    • @arandomlizard3411
      @arandomlizard3411 5 ปีที่แล้ว

      "fun" might not be the best word to describe it.

  • @mohamedsalbi2759
    @mohamedsalbi2759 3 ปีที่แล้ว

    Best channel ever

  • @Omran_90
    @Omran_90 5 ปีที่แล้ว

    does it have to be square nodes? what i have diagonal pathways like slopes. I'm talking about a 2D platformer game slopes.

  • @anma7424
    @anma7424 4 ปีที่แล้ว

    thank you very mutch

  • @amanmahendroo1784
    @amanmahendroo1784 4 ปีที่แล้ว

    Awesome videos! But could you please increase the font size of your code? That would be great

  • @dexyuzs
    @dexyuzs 8 ปีที่แล้ว +1

    Hi Sebastian, nice tut!
    The 'NodeFromWorldPoint' function works fine for me when the player is near the (0, 0) point (the center of the map, we can say). But it gives me unaccurate values if I put the player far from that point. Any ideas about what's happening?
    Thank you!

    • @moeayyad8228
      @moeayyad8228 5 ปีที่แล้ว +1

      Is it weird that I reply to a 3-year-old comment?
      Were you able to solve this? I kinda know where the problem is; if you're in a position that is larger than the grid world size (say your world size is set to 20 and you're at position 70), you're technically out of bounds, even though you're inside the grid, and that's because the division result will be larger than 1 which would then be clamped to 1 which is at the border of the grid. So we need some kinda offset from the origin, but so far all my attempts have failed :(

    • @moeayyad8228
      @moeayyad8228 5 ปีที่แล้ว +2

      I've actually just solved the problem. As I mentioned above, the problem was with the offset. I was trying to offset everything from the origin, but the solution was to actually pretend we've never left the origin. To do that, I subtracted the player's position with the object's position (so if the player is at 75,0 and the enemy is at 70,0 --> We can treat it as if the enemy is at the origin and the player is at 5,0), this way the function makes sense and resulting value would always be

  • @cuddlybunion341
    @cuddlybunion341 4 ปีที่แล้ว

    What Program did you use to make the animations?

  • @antonpolchenko6391
    @antonpolchenko6391 7 ปีที่แล้ว

    You realy good, how long you been working with C# and Unity?

  • @batomow
    @batomow 7 ปีที่แล้ว +1

    I know this might be a little bit off topic, but have you considered doing a GOAP AI tutorial? Since this sort of pathfinding is popularly implemented with GOAPs, i figured it could be an interesting follow up.
    Also, keep up the good work :)

  • @mahmoueco1200
    @mahmoueco1200 4 ปีที่แล้ว +2

    wow,just wow dude u r amazing where r u from !!! from land of genuis

  • @ashersmith3654
    @ashersmith3654 4 ปีที่แล้ว

    Thanks sm bro

  • @Ben274722
    @Ben274722 8 ปีที่แล้ว

    Hey Sebastian, great Video !!! But I have a question about the Node-Constructor in the Node class. I am a starter-programmer so forgive me any stupidity. Here is my Question : Why didn't you create the Node-Constructor in the grid-script and would this be possible too ? BR, Ben.

  • @RetroEcoChicken
    @RetroEcoChicken 4 ปีที่แล้ว

    add if (grid != null) if you are tired of the constant errormessages it just checks if it has a grid before trying to calculate
    {
    Node playernode = nodefromworldpoint(player.position);
    }

    • @RetroEcoChicken
      @RetroEcoChicken 4 ปีที่แล้ว

      then he deleates it in the end of the video. wow like getting my painting ripped from the refrigirator :(