Godot 4 3D - AI Pathfinding/Navigation

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024

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

  • @drobison100
    @drobison100 Год назад +283

    Godot 4.1 release made some changes to NavAgent methods, the docs will be your friend but simply:
    nav_agent.set_target_location(target_loc) is now nav_agent.target_position = target_loc
    nav_agent.get_next_location() is now nav_agent.get_next_path_position()
    Thank you for the content, please release more as often as possible, I eat it all up

    • @dootydut
      @dootydut 11 месяцев назад +1

      Thanks drobison100

    • @dobleve_walas
      @dobleve_walas 11 месяцев назад +1

      Thanks drobison100

    • @Karbonic
      @Karbonic 10 месяцев назад +1

      Thanks drobison100

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

      Thanks drobinson100

    • @Kman100003
      @Kman100003 9 месяцев назад +2

      This might explain my Getting a non_existent_function for 'get_next_location() function call

  • @AyoGodot
    @AyoGodot Год назад +106

    For anyone having errors with the nav agent functions, it has been changed to another method, e.g, set_target_location() is now set_target_position()

    • @iBurntMyCerealAgain
      @iBurntMyCerealAgain Год назад +21

      Yes,
      "var next_loctation = nav_agent.get_next_location()" ---> "var next_loctation = nav_agent.get_next_path_position()"
      "nav_agent.set_target_location(target_location)" ---> "nav_agent.set_target_position(target_location)"

    • @anton_garno
      @anton_garno Год назад +2

      @@iBurntMyCerealAgain omg, u a my hero

    • @GaryParkin
      @GaryParkin Год назад

      @@iBurntMyCerealAgain Thank you so much.

    • @GaryParkin
      @GaryParkin Год назад

      Thank you

    • @donkiel7111
      @donkiel7111 Год назад +1

      @@iBurntMyCerealAgain
      Line 17: Cannot call non-static function "get_next_path_position()" on the class "NavigationAgent3D" directly. Make an instance instead.
      Line 24: Cannot call non-static function "set_target_position()" on the class "NavigationAgent3D" directly. Make an instance instead.
      What should I do?

  • @animejoe4041
    @animejoe4041 2 года назад +39

    You are my new favorite person of the month! ;)
    I've been banging my head trying to figure out how to do this in Godot 4 and you came to the rescue! I could get my enemies to move from one point to the next point and that's it. They'd stop once they reached my starting location. I couldn't figure out how to get them to keep chasing me.

    • @DevLogLogan
      @DevLogLogan  2 года назад +4

      Happy you were able to find it useful! Thanks for watching. :)

    • @wowanimallia
      @wowanimallia 4 дня назад

      same problem i'm facing now

  • @GreySectoid
    @GreySectoid 2 года назад +21

    Nice example. Switching from Unity to Godot so these tidbits really help.

    • @DevLogLogan
      @DevLogLogan  2 года назад +5

      Thanks! Let me know if there’s any tutorials/examples in particular you could use. Looking for more ideas on what to make.

    • @GreySectoid
      @GreySectoid 2 года назад +4

      @@DevLogLogan Not sure if there's something comparable in Godot but here's some ideas: animation (state machines), collision detection (terrain vs objects), particle effects (and emitters), game state serializing (save/load functionality).

    • @BojanDam
      @BojanDam Год назад +2

      @@DevLogLogan Advanced Ai is onenof those ideas.

  • @GaryParkin
    @GaryParkin Год назад +32

    Thank you. this works perfectly.
    In Godot 4.0.2 Stable,
    3:15 nav_agent.set_target_location(target_location) change to nav_agent.target_position = target_location
    4:06 var next_location = nav_agent.get_next_location() change to var next_location = nav_agent.get_next_path_position()
    It should work find from there. Looks like the dev's changed from setters to properties.
    Also I needed to set my Agent Height Offset to .45 to make him stick to the ground.

    • @m3gasn1p3r
      @m3gasn1p3r 11 месяцев назад

      it's giving me the following issue:
      cannot call non-static function "get_next_path_position()" on the class "Navigationagent3d"
      what do i do about this..? am a total rookie I admit

    • @NewFunnyAnimations
      @NewFunnyAnimations 3 месяца назад

      Thank you! This also works in version: Godot_v4.2.1-stable

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

      You're the best, thanks. 👍

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

      @@m3gasn1p3r You're calling the function on the class itself (Navigationagent3d), not the instance (the nav_agent variable). Simply do nav_agent.get_next_path_position()

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

    Great tutorial and to the point. Little examples of "gotchas" too. I was able to set this up in an evening due to how little time it took to implement this solution. Thanks man.

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

      HOW?, IT TAKES ME LIKE TWO DAYS TO IMPLEMENT A FEAUTRE LIKE THIS

  • @plutoniumgamestudio
    @plutoniumgamestudio Год назад +7

    For anyone wondering, this is working in godot 4.0 stable, 1 thing you need to change is in the:
    func update_target_location(target_location) function, you need to change it to: func update_target_location(target_location) -> void:

  • @synthnostate
    @synthnostate Год назад +6

    Thanks! That was easier than expected. Momentum was a nice touch too.

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

    This was a perfect tutorial, no notes! Using this i cut out like 100+ lines of code that was basically doing this the hard way XD

  • @bodamat
    @bodamat 2 года назад +15

    Wow how simple it in Godot 4. But it wouldn’t so good if you don’t explain so clearly. Thanks you so much 🙂

    • @DamnMascus
      @DamnMascus Год назад

      Slava Ukraini!

    • @bodamat
      @bodamat Год назад

      @@DamnMascus Героям слава!

  • @Elrinth
    @Elrinth Год назад +4

    amazing tutorial, thanx for sharing! the enemies still move in like a triangle formation. looks hilarious :D

  • @TheMakuzao
    @TheMakuzao Год назад +6

    This is actually just perfect, thank you very much

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

    thanks for the help! Helped a lot

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

    Used this video to get avoidance working in my game. Thank you!

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

    The move_toward solution is what I needed to solve a problem I have had for ages, thank you for the suggestion

  • @ChrisKuglerDev
    @ChrisKuglerDev 10 месяцев назад +1

    Perfect and concise intro to this functionality. Thanks!

  • @bsunter
    @bsunter Год назад +2

    Brilliant. This video really helped me out. Keep it up!

  • @frankoesavage8314
    @frankoesavage8314 Год назад +5

    Thank you! You just saved a lot of hassle for me when it comes to pathfinding

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

    Does the navigation region check the children recursively to build the navmesh? E.g. if I have level object thing that has its own children, will it bake for the children too?

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

    This helped a lot thanks

  • @Trevex-UnKown
    @Trevex-UnKown 6 месяцев назад +1

    Thank you for the tutorial and the explanations it was really helpful!

  • @Realgooodmusic
    @Realgooodmusic 10 месяцев назад +5

    Does anyone know what the environment is? I can't find in Godot 4

  • @thomaseubank1503
    @thomaseubank1503 Год назад +5

    Wouldn't it be great if we could draw or manually edit our navmeshes?

  • @DR_GODOT_DEV
    @DR_GODOT_DEV Год назад +4

    thank you! nice job

  • @alextheguy4469
    @alextheguy4469 Год назад +6

    In Godot 4.0.3 stable, I was getting a nonexistent function error for the "update_Target_Location" function. It's not nav_agent.set_target_location" in 4.0.3. Instead, use "nav_agent.set_target_position" and it should work. Like others have said, similar thing with the _physics_process function. it's "var next_location = nav_agent.get_next_path_position" instead of "get_next_location"

  • @faiorb
    @faiorb 11 месяцев назад +1

    so it's that easy, huh?
    great video, very quick to show how to make it work

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

    so my enemy just moves to the centre of the map, instead of following the player?

  • @lucassenegrandchamp2015
    @lucassenegrandchamp2015 Год назад +5

    Hey, great tutorial! Helped me a lot setting up an enemy, thanks!
    I have one additional question, though:
    How do I make the node rotate to where it's going? I tried look_at() but it rotates suddenly, without any kind of smoothing. Tried using angle_to() and then a lerp_angle(), but the rotation is not right either.
    Cheers.

    • @deathscythearan9713
      @deathscythearan9713 Год назад +5

      var rotation_speed = 20
      $MeshInstance3D.rotation.y = lerp_angle($MeshInstance3D.rotation.y, atan2(-velocity.x, -velocity.z), delta * rotation_speed )
      Works good :)

    • @vas-poenitentiae-08
      @vas-poenitentiae-08 Год назад

      I’m just using a sprite3d with billboard mode activated.

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

    great tutorial

  • @bodhiiii
    @bodhiiii Год назад +1

    Crazy good tutorial! Thank you!

  • @Sean-gx1sf
    @Sean-gx1sf Год назад +6

    Interesting, what happens if there is no path to the destination? For example, the character is on an isolated platform that cannot be reached, what happens?

    • @deathscythearan9713
      @deathscythearan9713 Год назад

      The agent walks towards the target and stops when he can't go any further.
      "is_target_reachable()" to check if the destination is reachable

  • @Ceisriel
    @Ceisriel Год назад +1

    can you use navmesh in games or is used only for demos like gmesh? cause I've never seen anyone use navmesh with actual 3d terrain meshes... just squares and cubes.

  • @Karnivalh
    @Karnivalh 10 месяцев назад +1

    Your videos are f#cking amazing.

  • @vas-poenitentiae-08
    @vas-poenitentiae-08 Год назад +7

    How did you get the enviroment node? Has it been renamed?

  • @JorgeRosa
    @JorgeRosa Год назад +2

    Great! Thankyou! :)

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

    Thank you so much dude, you've helped me a lot!

  • @Chris-jp6ll
    @Chris-jp6ll Год назад +2

    Nice Tutorial! Any idea/tip how to handle if this 3 enemies just should walk to a specific location and stop there?
    I'm experimenting with this setup (but in 2D) and my agents push each other around (understandably) when they have reached the destination as only one of them can stand on the exact location. Just can't figure out a (for me proper) way how to handle this kind of "group to target movement".
    Yes, there's the "Target Desired Distance" Option of the NavigationAgent but that gets hardly a solution, the more Units are walking around.

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

    0:00 What node is the "environment?"

  • @paladin8843
    @paladin8843 Год назад +1

    I have no idea what I'm doing wrong but my screen just goes black or grey when I test with the added enemy.

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

    my nav mesh is saying it needs a bunchof stuff, only problem is my ground is a plane and when i try to give a collisionshape3d it wont let me use a plane, so i just put in a box and try to bake it and it crashses..

  • @Robin_H.I
    @Robin_H.I Год назад +2

    I tried the code and typed it in two times even changed location to position as the comments said but still nothing works. So if anyone sees this comment and knows how to help i would greatly appriceate it.

  • @gurkenpoo
    @gurkenpoo Год назад +2

    my enemies just go to the origin spawn of the player, they dont follow me:( i just printed what the "player.global_transform.origin" was showing, and it just print the spawn coordinates....when i move it doesnt change...that must be the error, but i can solve it. need help please.

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

      Exact same problem. Make sure that the CharacterBody3D is not a Child Of any other nodes. Fixed it for me.

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

      @@basscrowdead I had fixed it, and thats the way i did it. thank you very much for the help!

  • @bongo6_462
    @bongo6_462 Год назад +1

    Thank you so much :D

  • @shivanshdeepmaurya1081
    @shivanshdeepmaurya1081 Год назад +1

    Thank you very much!!!

  • @BlendFin
    @BlendFin Год назад +1

    Thank you!

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

    i followed this tutorial exactly and the character is navigating towards the enemy. idk how to fix this so if someone could help me out that would be great

    • @user-xsn5ozskwg
      @user-xsn5ozskwg 6 месяцев назад

      It sounds like you might have used the wrong scripts for the wrong scenes or possibly assigned things wrong in the world script if you're using similar code for the player and enemies.

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

      @@user-xsn5ozskwg OK thank you

  • @ThunderPlayStudios
    @ThunderPlayStudios Год назад +2

    thanks you. can you make more of these videos sir?

  • @khanhnguyenduy9455
    @khanhnguyenduy9455 10 месяцев назад +1

    Great tutorial! Can you do a tutorial for voxel terrain?

  • @rayenibex1537
    @rayenibex1537 Год назад +3

    too bad look like it not working in stable 4.0
    Invalid call. Nonexistent function 'get_next_location' in base 'NavigationAgent3D'.

    • @y2kman426
      @y2kman426 Год назад +5

      it changed to get_next_path_position()

  • @wowanimallia
    @wowanimallia 4 дня назад

    my enemy gets stucked after moving to a certain positon
    my enivironment is simple plane

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

    I can't get the npc to move at all, I've followed the other comments with the updated code but nothing works T_T

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

      I had the same problem, I fixed clicking "Bake NavigationMesh" button in "NavigationRegion3D" view, I forgot bake the nav region. I hope this fixes your problem as my :)

  • @tylerherr4288
    @tylerherr4288 3 месяца назад

    literally lost at the first step
    what is the environment node?
    the tutorial i was following has a level setup with a staticbody3d and meshinstance3d for visual and collisionshape3d for collision
    and i cant bake a nav mesh from that?

    • @tylerherr4288
      @tylerherr4288 3 месяца назад

      seeing all the other comments asking the same question
      guess ill just find another tutorial

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

    My enemies are not moving, in code its says it moves but it dosent. What can i do?

    • @user-xsn5ozskwg
      @user-xsn5ozskwg 6 месяцев назад

      How can you tell the code is telling the enemies to move? Is it possible you're missing the move_and_slide method?

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

    Im having a nonexistent function problem with 'set_target_location'

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

    My enemy doesn't move at all ._.
    It just stays string at me
    (I used an anímated sprite 3d instead of a mesh instance 3d

  • @JohnnyCashOriginal
    @JohnnyCashOriginal Год назад +2

    What icon is that environment node? I dont see anything like that in 4.0.2

    • @monet5889
      @monet5889 Год назад

      In case you haven't found it yet, it's a CSGCombiner3D

    • @JohnnyCashOriginal
      @JohnnyCashOriginal Год назад

      @@monet5889 thank you!

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

    anyone know how to make the enemies rotate towards you

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

    For some reason it says delta is never used in the function which sounds like it doesn’t make any sense

    • @user-xsn5ozskwg
      @user-xsn5ozskwg 6 месяцев назад

      If you never call delta it will be unused. It shouldn't cause any actual errors but to clean up your output add an underscore (the "_" symbol) in front of the parameter.

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

    i get this error:
    Invalid operands 'Vector3' and 'float' in operator '+'.
    anyone know how to help?

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

      you can't add a Vector3() to a float.

  • @thomaseubank1503
    @thomaseubank1503 10 месяцев назад

    The only thing this tutorial is missing is that sometimes the player may exit the NavigationRegion3D. When this happens the game lags out unbearably so.

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

      Thanks, I didnt know what was going on...

  • @jctoons9707
    @jctoons9707 11 месяцев назад

    I was experiencing an error on var new_velocity = (next_location - current_location).normalize() * speed
    I dont know how fix it

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

      .normalized() dont forget the 'd'

  • @Ebbyplay
    @Ebbyplay Год назад +2

    Great Tutorial! You would make it more easy if you gave access to the test world files :)

  • @djlanav
    @djlanav Год назад

    For some odd reason, my "current_location" and "next_location" are always the same and as a result my enemies never move. My code is practically the same as in the video. I'm using Godot 4.0.2 stable. I can show my code and what not if need be

    • @djlanav
      @djlanav Год назад

      Nevermind lol! Turns out it was the "Agent Height Offset" that was the problem

  • @Alexander_Meyer
    @Alexander_Meyer 11 месяцев назад

    What about rotation?

  • @GamingsMostAmazingSoundtracks
    @GamingsMostAmazingSoundtracks 11 месяцев назад

    Yo dude the NavigationAgent3d node is gone in my current version. Is it now called something else

  • @madduckling4436
    @madduckling4436 Год назад

    its good and all and I appreciate the tutorial however how am I supposed to rotate the enemy towards the player

    • @mattvonfat
      @mattvonfat Год назад +3

      I don't know if this is the best way, but you can use the look_at method. In the example in this video I think if you put look_at(next_location) in the _physics_process function it should work.

    • @madduckling4436
      @madduckling4436 Год назад

      @@mattvonfat Thank you I'll try it and see if it works , regardless if did or not I appreciate your reply fam :)

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

    My enemy just yeets himself off the map and continues to go forever im at a loss. Can anyone help?

    • @user-xsn5ozskwg
      @user-xsn5ozskwg 6 месяцев назад

      Your target location might be wrong. If you're following this tutorial exactly make sure that in the _physics_process method of the world script you correctly reference the player and their location (lines 3 and 6 at 6:20 in the video)

  • @MoogieSRO
    @MoogieSRO 23 дня назад

    Soooo, nobody else in the world is getting "Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a cell_size of 0.00999888 (bunch of numbers) while assigned to a navigation map set to a cell_size of 0.25"? Just me? Alrighty then. Also had to defer the enemy's _physics_process for 1 frame because it was trying to use the navmap before it was initialized, nobody got that one, either? Cool, cool.

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

    I LOVE YOU 💞💞💞💞💞💞💞💞💞💞💞💞💞💞

  • @Aspen72
    @Aspen72 Год назад +2

    doesnt work :(

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

    Joka = 🌳

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

    anyone know what they changed _physics_process(delta) to?

    • @user-xsn5ozskwg
      @user-xsn5ozskwg 6 месяцев назад

      That method still has the same name, what issues are you having?

  • @srers357
    @srers357 22 дня назад

    in my project bluds just standing there not moving

    • @code-nk
      @code-nk 4 дня назад

      were you able to solve it

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

    Can i give you a positive criticism?
    When i need to learn something and i find a tutorial where the creator goes like "we will do this, but i will explain what this is for later", i usually will inmediately start looking for another tutorial because i would rather stop the tutorial for a couple of minutes to learn what that thing is before countinuing doing stuff because from that point i no longer understand what i am doing

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

    Can someone help me before my head explodes, so my ai just moves forward to where I am but doesn't follow me, why is that?
    #this is my target code
    extends CharacterBody3D
    var health = 100
    var speed = 4.0
    @onready var nav_agent = $NavigationAgent3D
    # Called every frame. 'delta' is the elapsed time since the previous frame.
    func _physics_process(delta):
    # AI movement
    var current_location = global_transform.origin
    var next_location = nav_agent.get_next_path_position()
    var new_velocity = (next_location - current_location).normalized() * speed
    velocity = new_velocity

    # Update velocity
    velocity = velocity.move_toward(new_velocity, 25)
    move_and_slide()
    # Death
    if health

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

      fixed it, I wasn't referencing the node properly.

    • @code-nk
      @code-nk 4 дня назад

      @@memze1148 help me out ...please

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

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

    I kepp getting the _physics_process() "delta" error

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

    Very nice video, but change that keyboard 😅

  • @chosendart1482
    @chosendart1482 Год назад +1

    can someone please tell me whats wrong with ths code ts not working :/
    extends Node3D
    @onready var player = $Player
    func _physics_process(delta):
    get_tree().call_group("enemies", "update_target_position", player.global_transform.origin)
    extends CharacterBody3D
    @onready var nav_agent = $NavigationAgent3D
    var SPEED = 3.0
    func _physics_process(delta):
    var current_location = global_transform.origin
    var next_location = nav_agent.get_next_path_position()
    var new_velocity = (next_location - current_location).normalized() * SPEED

    velocity = velocity.move_toward(new_velocity, .25)
    move_and_slide()
    func update_target_position(target_position):
    nav_agent.set_target_position(target_position)

  • @KumokoChan
    @KumokoChan 11 месяцев назад +2

    Can you bake the NavMesh in script? For like, a procedural terrain.

  • @guillermoantonioventuragon1576

    Hello, for some reason when I try to enter nav_agent.get_next_path_location() It gives me an error: Invalid call. Nonexistent function 'get_next_path_location' in base 'NavigationAgent3D'. What am I missing?

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

    extends MeshInstance3D
    var SPEED = 3.0
    func _physics_process(delta):
    @onready var nav_agent = $NavigationAgent3D
    func update_target_location(target_location):
    nav_agent.set_target_location(target_location)
    var next_location = nav_agent.get_next_location
    var new_velocity = (next_location - current_location).normalize() * SPEED
    var current_location = global_transform.origin WHAT AM I DOING WRONG?!?!?!

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

      Godot 4.1 release made some changes to NavAgent , look at first comment to fix the

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

      uh, u used mesh instance? or just the script is in it?

  • @Simon2d3d
    @Simon2d3d Год назад +1

    target reached did not work until I added this below 'var SPEED = 3.0'
    func _ready():
    nav_agent.connect("target_reached", Callable(self, "_on_navigation_agent_3d_target_reached"))
    Thanks GPT 4 ;)

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

    E 0:00:00:0498 monster.gd:9 @ _physics_process(): NavigationServer map query failed because it was made before first map synchronization.
    Condition "map_update_id == 0" is true. Returning: Vector()
    modules/navigation/nav_map.cpp:119 @ get_path()
    monster.gd:9 @ _physics_process()

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

      Having same issue😢

  • @chifodic6758
    @chifodic6758 11 месяцев назад +1

    SO USEFULL THANKS

  • @Dignelson
    @Dignelson 13 дней назад

    Thank you so much mate!

  • @user-lw8fk9hm9i
    @user-lw8fk9hm9i Год назад

    Does anyone know how to make a ai where if it's in range it will chase otherwise it will look around

  • @lionson5253
    @lionson5253 Год назад +1

    THANK YOU SO MUCH