Point & click 2D grid movement in Godot 4 using AStarGrid2D

Поделиться
HTML-код
  • Опубликовано: 17 сен 2023
  • Assets: www.kenney.nl/assets/monochro...
    In this tutorial we'll take a look at how to move a 2D character to a position clicked by the cursor whilst using AStarGrid2D to also dodge obstacles.

Комментарии • 101

  • @JorgeFigueroa-lx6rh
    @JorgeFigueroa-lx6rh 16 дней назад +1

    This video was so valuable to me right now after 2 days trying to figure out how to properly do isometric grid movement and running into walls. By obligation, I have to leave this comment to let you know that a random stranger on the internet appreciates you. Thank you, so much!

  • @domedevep
    @domedevep 9 месяцев назад +23

    ohhh dude, i just changed the movement styled from a grid to a free movement because i couldnt do this :(. Keep making tutorials like this, you're so valuable for newbie game devs like me!

  • @alistormccoy1953
    @alistormccoy1953 5 месяцев назад +3

    Heya, just popping in to say that AstarGrid2D has a property "offset" which allows you to set the local coord for each tile during setup instead of having to loop over and adjust them individually like you did near the end ruclips.net/video/DkAmGxRuCk4/видео.html Cheers!

  • @Automageddon
    @Automageddon 6 месяцев назад +1

    I've been struggling with this for days until I found this video, thank you!

  • @Rafael-wr7wi
    @Rafael-wr7wi 3 месяца назад

    Great tutorial, finally understood AStargrid2D after your video

  • @NicoFye
    @NicoFye 9 месяцев назад +4

    Instant like for showcasing the result at the start of the video

  • @zirklutesKerpa
    @zirklutesKerpa 8 месяцев назад +1

    This is an amazing tutorial, thank you so much!

  • @prechabahnglai103
    @prechabahnglai103 9 месяцев назад

    Been looking for this. THANK YOU SO MUCH!

  • @BadgeyOR
    @BadgeyOR 2 месяца назад

    Thank you for the tutorial it really helped there wasn't even one for Arrow keys. And now for Point and click Thank you so Much.

  • @jorie100
    @jorie100 8 месяцев назад +3

    i LOVE YOU, THIS IS EXACTLY WHAT i WANTED. It was very interesting because my game has 3d graphics and I was able to use this using a gridmap instead, converting the vector2 to vector3 and viceversa, and shooting a raycast from the camera to know the position. Since my 3d game doesnt have height or layers of land, I only nedeed to move the character on a 2D plane and I also nedeed to do it with squares and without diagonals just like you but in 3D, and this solution is working wonders. Thank you so much.

  • @KommissarAlexander
    @KommissarAlexander 4 месяца назад +1

    Amazing work, I was breaking my head trying to do / find somethjng like this

  • @nyx3m
    @nyx3m 9 месяцев назад +3

    You are doing a great job , taking on the topics which aren't yet touched, please keep making videos for our new thriving community. We all are rooting for you 🎉

  • @kindavacant7843
    @kindavacant7843 8 месяцев назад +4

    Great overview, wish I'd seen it five months ago when I was trying to work it out. Using single tiles instead of atlas one can define a 'solid' via tile number instead of 'walkable' which is handy for an active map where tiles can change from not solid to solid. 'Tweening' (IE: tween.tween_property($%playerSprite,"position",nextTileLoc,.4) can also be used instead of process.

  • @dougsaylor6442
    @dougsaylor6442 7 месяцев назад +1

    Really great video! This helped a lot. :)

  • @ParryMime
    @ParryMime 7 месяцев назад

    Excellent tutorial!

  • @JohnSane
    @JohnSane 3 месяца назад +1

    Awesome! Now a third video would be great which combines the A* Point & Click with the 2D Movement video. Keep up the good work!

  • @AlexandroTrevisan
    @AlexandroTrevisan 9 месяцев назад

    Nice and to the point! I like that!

  • @plyr0
    @plyr0 5 месяцев назад

    Great tutorial! Thank You!

  • @Gandalf_Le_Dev
    @Gandalf_Le_Dev 9 месяцев назад +1

    Great tutorial, thanks :D

  • @jablao
    @jablao 6 месяцев назад

    great video, helped me a lot, thank you

  • @hachetrescomacatorce2340
    @hachetrescomacatorce2340 16 дней назад

    you are awesome bro, thanks!!! greetings from colombia

  • @brandonjacksoon
    @brandonjacksoon 5 месяцев назад

    Thanks for the tutorial! Liked and subscribed!)

  • @CaptmagiKono
    @CaptmagiKono 7 месяцев назад

    Thanks, this was very helpful.

  • @levilivengood4522
    @levilivengood4522 8 месяцев назад

    thanks. very helpful for the fallout 2 project i'm working on

  • @ichlalsulbulqiah1974
    @ichlalsulbulqiah1974 2 месяца назад

    Thank you so much for your tutorial, it's really helpfull

  • @DrNabeel20
    @DrNabeel20 7 месяцев назад

    Super usful! thank you very much

  • @AndrewKwiatkowski
    @AndrewKwiatkowski 9 месяцев назад +6

    This is quite amazing, so many really rubbish tutorials on here. This one is very well made. very clear. How about a tutorial on how one would do this on a 2d map that has gravity. Rather then North/South, a Up and Down type view point

  • @timflatus
    @timflatus 5 месяцев назад +1

    Great tutorial. You may have convinced me that AStarGrid isn't the way I want to go as it looks complicated, which often means computationally expensive. I have a lot of moving objects.

  • @ArgonZavious
    @ArgonZavious 7 месяцев назад

    Thank you!

  • @sailorjune2121
    @sailorjune2121 7 месяцев назад

    This is why it’s important to read the docs
    I spent all yesterday trying to implement a star from scratch 💀💀💀
    Godot rules

  • @adamkerr6853
    @adamkerr6853 8 месяцев назад +1

    Hi, thanks for this. Just what I needed. I'm having a small bug where the top part of the tile map doesn't return a click value (top 3 rows or something) but the ones underneath all seem to work just fine. I could design around it I suppose but I don't suppose you know what could be causing it do you... (they're all the same tile and this was a problem introduced before the "walkable tile" was introduced.)
    I will add that is this the first thing I did in the project and the only difference I've made so far is my tiles are 32 x 32 which the code is adjusted for.

  • @CaptmagiKono
    @CaptmagiKono 7 месяцев назад +2

    Oh yeah, just one minor thing after finishing the line-drawing part. You can have the line get removed from the screen when arriving at the destination by just clearing the "current_point_path" array of values. Worked for me just putting "current_point_path.clear()" on the line before setting "isMoving" to false.

    • @user-dd8jn1fm8r
      @user-dd8jn1fm8r 21 день назад

      I just move the line on -10 z index when finish moving

  • @purplepixeleater
    @purplepixeleater 9 месяцев назад +6

    any chance we can get a tutorial on how to do this with isometric tilemaps? Does it work the same?

    • @CptProtagonist
      @CptProtagonist 3 месяца назад +1

      currently testing this with an isometric map and it seems to be working the same so far

  • @thecumscrubber
    @thecumscrubber Месяц назад

    This is the best tutorial for AStarGrid2D, it has really helped me out. Would anyone know how to how to not allow diagonal movement when moving sideways but allowing diagonal movement when moving up and down? My issue is I am uing a isometric grid, and using hexagon shapes. This is also affecting how the draw_polyline is showing up on screen (but that may be fixable), so not as concerned about it yet.
    Thanks!

  • @user-dd8jn1fm8r
    @user-dd8jn1fm8r 21 день назад

    Great tutorial! But you can use "if not id_path.is_empty()" instead of "if id_path.is_empty() == False", I think its a little faster if you have big script

  • @CutyDina
    @CutyDina 4 месяца назад

    love this tutorial! is there a way to keep moving until it crashes with any obstacle? like sliding?

  • @vegetablegames3295
    @vegetablegames3295 2 месяца назад +1

    Hello I really appreciate the tutorial, but do you know how to add a limit to how far the player can travel?

  • @MrHsktoto
    @MrHsktoto 5 месяцев назад

    great explanation even as a godot beginner it feel fluid and thanks for that . i'm struggling to get it work with a tile based movement at the same time both together result in a problem with the var is_moving = bool because i already us it at var is_moving = false for the tiles based movement how could i combine this two together ?

  • @Hauness349
    @Hauness349 6 месяцев назад +1

    How would you write the proper function to only allow you to move one space (or "action" is what I'm going to call it in the game) every 5 seconds.
    Also, do you have any recommendations for learning these different functions and variables so I can know what I'm looking for in the future and don't have to bug you or rely on tutorials?

  • @ubit123
    @ubit123 8 месяцев назад +1

    is_moving is redundant. If Targetposition is not null you are moving. When you reach intermediate destination you pop new target position. When you reach final position, there is nothing to pop and you set target position to null.

  • @SkylonYT
    @SkylonYT 28 дней назад

    This is the movement I'm looking for my game too, but can't find any tutorials on how to make the character face the direction they're moving. Can you make a tutorial on it, or give some advice on how to animate in this movement style?
    Thanks in advance!

  • @deepfriedtuba
    @deepfriedtuba 2 месяца назад

    Could you make a video applying this to multiple selectable agents? Like RTS style movement

  • @spiritnet6219
    @spiritnet6219 8 месяцев назад

    Hi, very interresting implementation.Just one question plz: how do you manage obstacle ?

  • @cr0wmatic
    @cr0wmatic 7 месяцев назад

    would anything change when using a hex tilemap instead of a square tilemap?

  • @user-sj3jr2rp8w
    @user-sj3jr2rp8w 5 месяцев назад +2

    Hi, great video you made. Will there be a sequel video about animation on this code using AnimationTree? And I would like to see how to use flip_h, how to change the movement speed, and how to leave the character position back-idle, front-idle, side-idle after movement.
    I tried animating the character, for some reason it doesn't work. I looked for a video - I couldn't find one!

    • @agam3z153
      @agam3z153 Месяц назад

      did u solve the problem ?

  • @StandAlone3
    @StandAlone3 4 месяца назад

    I'm running into a weird issue where the first time I go to move the character, it sets the target position to (0, 0) for the first couple of seconds, then it corrects itself and starts going the right way. After that it doesn't do it again on subsequent moves.

  • @FuzzballStudios
    @FuzzballStudios 9 месяцев назад +1

    I think the preferred syntax for checking if a boolean is false is actually `if not current_id_path.is_empty():` rather than checking if it’s equal to `false`.

    • @ubit123
      @ubit123 8 месяцев назад

      Wanted to say the same 👍. No way I would let this pass through code review 😊

    • @DanJuega
      @DanJuega 5 месяцев назад

      @@ubit123 why tho

  • @abdou023
    @abdou023 2 месяца назад

    I'm facing a problem that the path doesn’t appear after the first click. The navigation itself works perfectly but I have to press the mouse a second time in order for the path to show and after that it works fine.

  • @IVThesimple3436
    @IVThesimple3436 2 месяца назад

    was wondering if anyone knows how I can set the blend position of an animation tree using this method for movement so the game knows which direction the player is moving? I attempted to use velocity but it seems the engine doesn't think the player has any velocity using the movetowards method for global position, I attempted to use the player having no velocity to set the idle state as a test and the game always thought the player was idle, though this could be an error on my part I attempted to set the blend position based on the players velocity.x and velocity.y normalized and the animation never changed which again leads me to believe the engine does not detect this method as change in velocity, any help as to how I can set the blend position would be much appreciated

    • @agam3z153
      @agam3z153 Месяц назад

      did u find it out ?
      i need to know how which direction the player is moving so i can play the animation right and left

  • @Dustifer
    @Dustifer Месяц назад

    Ive been following this, basically, and its been very helpful. I have one small problem...
    So, I changed it so per mouse click you only move 1 tile, like your previous video with the wasd keys
    But the first move can still be diagonally for some reason, every move afterwards can not. Ive been at it for 2 hours and have not been able to fix it... any idea's?
    Edit: actually, every first click the movement is incredibly wierd and dubble and off center and slightly diagonally. I have no idea what is going on because afterwards everything is normal...

  • @andresgrimminck8674
    @andresgrimminck8674 2 месяца назад

    Do you know if it is possible to put the cost to every tile and let the algorithm take that into account when finding a path?

  • @Drachenbauer
    @Drachenbauer 5 месяцев назад

    the player sprite should better be an extra graphic with transparent background to prevent vovering grass-pattern outside the actual character´s body.

  • @hamtown4757
    @hamtown4757 Месяц назад

    Hi, im doing this in isometric view, and for some reason whenever i click on the tile directly next to the player it moves to a tile that is diagonal instead. im guessing the mouse is off or something. im kinda lost.

  • @NomarOmens
    @NomarOmens 6 месяцев назад +1

    Hi, I enjoyed this tutorial but encountered an issue where you cannot move to the solid objects but the player character will still move THROUGH non-walkable objects to get to their destination. My code is the same as in the video but I will update this comment if I find out what's wrong.

    • @OhNoes_TV
      @OhNoes_TV 10 дней назад

      Mine walks right through them too

  • @JD-us8xw
    @JD-us8xw Месяц назад

    Is there a way to adapt this to run on multiple tilemaps?

  • @TheIronicRaven
    @TheIronicRaven 8 месяцев назад

    Any chance you would make this for 3D? For GridMap?

  • @Epeters022
    @Epeters022 9 месяцев назад

    awesome tutorial! It was really helpful setting up my ai to move around the tilemap. My only problem is that the ai ignores collision with each other. Any tips for how i could dynamically update the tile an ai currently occupies so the path updates around it? All my attempts to use set_point_solid seem to have no effect.

    • @RetroBright262
      @RetroBright262  9 месяцев назад +1

      Sounds like an interesting subject for the next video, I already have a project set up for this so stay tuned 🤔

    • @Jonathan-sk5wy
      @Jonathan-sk5wy 9 месяцев назад +1

      Make updating the collision info on the map part of the movement function!

    • @martin-gaming
      @martin-gaming 8 месяцев назад +1

      I found a way to disable the tiles the unit is on bit this can result in huge spaces being blocked, especially if units are smaller than the tile and an entire tile gets blocked or bigger than a tile then it can disable multiple

    • @Epeters022
      @Epeters022 8 месяцев назад

      I ended up using a similar approach. My biggest issue is when 2 actors intersect they end up course correcting in the same direction in an infinite loop and never reach their destination@@martin-gaming

    • @martin-gaming
      @martin-gaming 8 месяцев назад +1

      @@Epeters022 I'd say you let the pathfinding as it is with terrain/wall Collison and crrate a diffrent system for unit avoidance and optional some smoothing, steering behaviors, etc

  • @longuemire748
    @longuemire748 7 месяцев назад +5

    Thanks for this tutorial,
    Does it also work for isometric?

    • @RetroBright262
      @RetroBright262  7 месяцев назад +6

      Isometric tile maps require a separate implementation; expect a video soon. 🙂

    • @longuemire748
      @longuemire748 7 месяцев назад

      @@RetroBright262 Thank you :), The code is very intuitive.

    • @JoksterStudios
      @JoksterStudios 7 месяцев назад +1

      great tutorial - just followed it on an isometic tileset and worked like a charm except the final path drawing@@RetroBright262

    • @Voxet1
      @Voxet1 6 месяцев назад

      i will say the pathing was a little odd, alot of the time it felt like it wasnt using the best possible path at all @@JoksterStudios

    • @agam3z153
      @agam3z153 Месяц назад

      @@RetroBright262 can u make an animation guide for this tybe of movement plz i need it so bad

  • @ubit123
    @ubit123 8 месяцев назад

    15:37 why add offset? Can you iterate from tilemap.get used rec.position.x to tilemap.get used rec.size.x?

    • @Myko82
      @Myko82 8 месяцев назад

      That would be too many tiles, but you could iterate from rec.position.x to rec.end.x

  • @Lets_Get_This_Bread
    @Lets_Get_This_Bread 4 месяца назад

    No matter what I do I get an error on
    astar_grid.cell_size = Vector2(32, 32)
    Only difference is I am using a 32 x 32 tileset, any assistance would be appreciated

  • @NoRageStudios
    @NoRageStudios 5 месяцев назад +1

    Thank you for the tutorial, is there an easy way from your script to be able to move the character if we are still pressing the mouse button ? I mean, I want the player to "follow" the cursor if I am still pressing the mouse button. Thank you

    • @NoRageStudios
      @NoRageStudios 5 месяцев назад +1

      I found a solution, if someone need it
      1. remove the func _input(event) function and its content.
      2. add those two functions in your script :
      func _process(delta):
      if Input.is_action_pressed("move"):
      get_player_path();
      func get_player_path():
      var id_path = astar_grid.get_id_path(
      tile_map.local_to_map(global_position),
      tile_map.local_to_map(get_global_mouse_position())
      ).slice(1)

      if id_path.is_empty() == false:
      current_id_path = id_path

    • @agam3z153
      @agam3z153 Месяц назад

      @@NoRageStudios did u know how to play animations in different directions
      because i don't know how to get direction to play right and left animations

    • @renardfele
      @renardfele 29 дней назад

      ​@@agam3z153 I am sorry I haven't worked on that project since a long time and I had to reinstall windows. I haven't kept my Godot Projects 😟

  • @ReddleyYT
    @ReddleyYT 6 месяцев назад

    I'm getting an error that says "Invalid call. Nonexistent function 'get_call_tile_data' in base 'TileMap'.".
    I'm at 14:46 in the video trying to run it just as you did a moment later.
    I'm new to scripting and Godot, so things are a bit confusing for me at the moment.
    Though, it sounds like to me it could be because my TileMap is set up incorrectly, or perhaps it's because it's a child to NavigationAgent2D, idk.

    • @MrHsktoto
      @MrHsktoto 5 месяцев назад

      try to add @onready var tile_map : $"../tilemapdirectoy" and if you already have it check if the path is good and if the script find it way to this actual path that might be the problem

  • @puleroxd4116
    @puleroxd4116 3 месяца назад +1

    My code says invalid operands Vector2 and Vector2i in operator ==. I only managed to do the tutorial until 10:13

    • @puleroxd4116
      @puleroxd4116 3 месяца назад +1

      fixed apparently my global position was not 0,0 but 167,198 so all i had to do was subtract that and for some unknown reason that fixed the Vector2 and Vector2i... not exactly sure how that works but its working now

  • @shahnet2201
    @shahnet2201 9 месяцев назад

    Hi can you make a grid#tile movement like advanced war game

    • @iluviatharelbardo8901
      @iluviatharelbardo8901 8 месяцев назад

      hello bruh. I'm trying to start something similar to an advance war too. I suppose you can use this AStargrid in the tilemap and find a way for the units to interact with it by blocking paths, instead of having a grid for each unit. Otherwise I don't know how I would detect the other units in the game, allies and enemies. I'm still learning

  • @rockmystyle
    @rockmystyle 8 месяцев назад

    Nice video, but I found a bug. The feature when you cant change target during moving doesn't work with your code. Just put inside the 'input' function one line and it will work perfect:
    if is_moving == true:
    >return

    • @RetroBright262
      @RetroBright262  8 месяцев назад

      The project shown in the tutorial specifically allows you to overwrite the path array during movement, but only navigates to the new target after it has finished moving to its current target when already moving. If you don't want the player to change target whilst moving than what you have proposed might work. It's not a bug, it's a feature.

  • @sajberkg
    @sajberkg 2 месяца назад

    is the code available somewhere? :)

  • @pepsipwns666
    @pepsipwns666 8 месяцев назад

    Wondering how well this scales to large 500*500 tile maps, I used the NavigationAgent/Tilemap Navigation and the grid ended up lagging at a large size clicking a far tile. So I ended up baking a navigation across my entire map (not grid based). I wonder if I can achieve the grid movement with this, is it more performative? Have you tried with a larger map size?

    • @werewolfdragon4740
      @werewolfdragon4740 7 месяцев назад

      probably pretty late but I can give you a relative answer, for any potential future searchers. .
      The problem is that with pathfinding the longer a distance the "character" needs the travel, the exponentially more blocks need to be calculated. for instance, searching in a 50x50 block will require approximately half the squares searched to find the "correct" square if they're on opposite sides, that would be 1250 blocks checked for one single pathfinding operation. a 500x500 could involve operations upward of 12500 blocks searched, small wonder that there's lag involved, the algorithm by default isn't really designed for sizes that large.
      For a built-in solution, if you enable jumping (set_jumping_enabled ( bool value ))
      then the program will assume that there is no weighting in the pathfinding (which means it will not check each individual tile and instead make straight lines across gaps of non-blocked tiles.
      this comes with the drawback of the pathfinding completely ignore weighting (which is obviously not a problem if you don't include any)
      if you're not using the built-in pathfinding, or weighting, this will obviously not help.
      so instead I would recommend giving a pathfinding distance limit, say 100 blocks so that it loads the pathfinding in chunks and merely walks in the correct direction until the final "path" can be found. unless your terrain is extremely convoluted, this should be a good solution for any small to medium size game. (as in not years of progress)
      alternatively to the first solution, you can turn off jumping for short distances, and turn it on for long ones. I don't know how effective this would be, but it seems like it should help.
      other than that I would recommend writing a pathfinding algorithm of your own that is better optimized for dealing with your particular troubles, I can't really provide any advice for how to tackle that, but it's pretty much the nuclear option, and shouldn't be necessary unless you really just have a maze of a level with a lot of intricacies.
      if you are unsure what weighting is: a weight in pathfinding is effectively the "cost" to go one block. it's one to go in any direction on any tile by default. a pathfinding algorithm will always try to find the lowest "cost" path. going around tiles that cost a lot in favor of cheap tiles.
      this is useful when considering tiles that would slow the player down, as you can increase their weight to make the algorithm naturally avoid them unless it would be faster to simply go through.
      you can also change it to where going diagonally is slightly more expensive, to compensate for it being a longer distance, as with the default method, diagonals are extremely preferred due to covering twice the distance at no extra cost (1.5x cost is the standard approximate)

    • @pepsipwns666
      @pepsipwns666 7 месяцев назад

      Thanks for your very detailed response. I basically came to this conclusion that it was too many tiles to calculate. I ended up baking a navigation mesh over my map instead of per tile, so its large polygons that fill the map, the movement is more 'diagonal' than I would like, but it certainly reduces the number of steps in the path drastically, so I no longer have lag.
      I did use the 'jumping' technique with the first solution and it still had lag. I probably could have done what you said about chunking the path into smaller sections, that's a great idea. @@werewolfdragon4740

  • @bizidru
    @bizidru 9 месяцев назад

    why didn't you want to use it Line2D? It's much easier to use line drawing

  • @peace__duke
    @peace__duke Месяц назад

    source code pls

  • @xanmal7042
    @xanmal7042 18 дней назад

    You know whats frustrating? needingf help on a video and scrolling down just to see tons of praise.. I agree its great, but its hard to find ANY help on a video when all you see are people being yes men!

  • @agam3z153
    @agam3z153 Месяц назад +2

    14:56 i but " , " after y and it worked
    great tutorial btw 🤍
    can u make tutorial about animations in this style of movement ?

  • @papierk0rp
    @papierk0rp 9 месяцев назад

    This was really helpfull thanks a lot.