Pathfinding in Godot 4 is Easy - Navigation3D Tutorial

Поділитися
Вставка
  • Опубліковано 23 жов 2024

КОМЕНТАРІ • 133

  • @abradotcs
    @abradotcs  Рік тому +4

    Wishlisht my game Moving In: store.steampowered.com/app/2307660/Moving_In/

    • @Brr0nzy
      @Brr0nzy 3 місяці тому

      so im getting an error when doing Global.target.global_position its saying Global isn't declared I thinks its a variable does it not and what do I do to fix it?

  • @UltProf
    @UltProf Рік тому +108

    With this, every father can now navigate to the store for milk and come back on the fastest route possible. Impressive!

  • @Saas_1
    @Saas_1 Рік тому +12

    Berry helping me navigate through the world of 3D navigation in Godot

  • @johannjomy8764
    @johannjomy8764 5 місяців тому +15

    for the people who cannot figure out the code for the target and want to do everything in the character body:
    extends CharacterBody3D
    # speed vars
    @export var speed = 13
    @export var accel = 10
    # the vars which make you do the thing where you do the thing
    @onready var nav: NavigationAgent3D = $NavigationAgent3D
    @onready var target: Marker3D = $"../target"
    # the stupid physics stuff
    func _physics_process(delta):

    var direction = Vector3()

    nav.target_position = target.global_position # this one needs the @onready vars we defined earlier

    direction = nav.get_next_path_position() - global_position # and so does this
    direction = direction.normalized()

    velocity = velocity.lerp(direction * speed, accel * delta)

    move_and_slide()

    • @MohamedASKAR-zj1sb
      @MohamedASKAR-zj1sb 5 місяців тому +1

      Can you create the same code but the enemy follows the player?

    • @noodledealer7447
      @noodledealer7447 5 місяців тому +3

      @@MohamedASKAR-zj1sb Instead of using the marker3d use the player's position. get the player's position by for example a raycast or area3d node.

    • @yipyiphooray339
      @yipyiphooray339 4 місяці тому

      @@noodledealer7447 can't I just create a variable that stores the refernce to the player, then just do, "player.position"?

    • @noodledealer7447
      @noodledealer7447 4 місяці тому

      @@yipyiphooray339 thats also an option. But you have to keep updating the player's position right?🤔 For example by using the area3d's signal method; on body entered. or use the raycast and get the player's position where it collides with the player.
      I don't know if there any other efficient methods

  • @aneckdope
    @aneckdope Рік тому +7

    just started doing AI for my game, this video comes in clutch, thank you strawberry person

  • @JRHainsworth
    @JRHainsworth Рік тому +19

    "Global" isn't declared in the current scope.

    • @WalkerRileyMC
      @WalkerRileyMC 6 місяців тому +3

      "Global" is generally used as a custom made autoloaded script, which I assume is what he's using here. If you're not familiar with autoloading scripts, I suggest taking a few steps back from this video and learning about those first.

  • @bruceparker6142
    @bruceparker6142 Рік тому +13

    You did not show the Target script.

  • @bobojenkins5805
    @bobojenkins5805 5 місяців тому +19

    "Global" not declared in current scope

  • @lennertplayz
    @lennertplayz 7 місяців тому +3

    Please explain, what is this target node that just appeared in your scene tree?

  • @DarkGigalaxie
    @DarkGigalaxie 9 місяців тому +9

    for me the exit say: Identifier "global" not declared in the current scope. what ?

  • @jampre
    @jampre Рік тому +3

    You seriously just saved my game! thx!!

  • @mmettler42
    @mmettler42 2 місяці тому

    Thank you for such a simple and straightforward video! :)

  • @SpringDavid
    @SpringDavid Рік тому +8

    Is the "target" code supposed to have something to make the "global.target.global_position" not become a error?

    • @flyingjudgement
      @flyingjudgement Рік тому +2

      Hi did you find a solution? Update Solution is in "Robin_H.I
      " Discusion, a bit downer in the comments. Target was not specified

  • @Mepa-xb4gd
    @Mepa-xb4gd 10 місяців тому +18

    You never show how target_position is defined in your globals

    • @TQoE_2021
      @TQoE_2021 8 місяців тому +4

      In the target script in a process function. Global.target_positioning = transform.origin

    • @DrEasy1
      @DrEasy1 4 місяці тому

      nav.target_position = player.global_position

  • @Eener1000
    @Eener1000 Рік тому +2

    I remember in Unity that pathing was very easy to do...untill any Z-axis gets involved (like for a flying game) and back then I searched for hours on forums to find any help but never found it. Here is hoping that I have more luck in Godot.

    • @s3nju279
      @s3nju279 11 місяців тому +2

      navmeshes are for flat surfaces. you prob want to make your own follow system using ray casts if theres a flying enemy that is following you. (like raycast if the object infront is the player if not go around) unity and godot do not have volumetric nav meshes as far as I know.

  • @s3nju279
    @s3nju279 11 місяців тому +5

    error: NavigationServer map query failed because it was made before first map synchronization.

    • @kevinkaranja8470
      @kevinkaranja8470 6 місяців тому +2

      add await get_tree().physics_frame in your physics process before setting the target position for the navigation agent.

    • @patti6879
      @patti6879 3 місяці тому

      @@kevinkaranja8470 this worked for me ty, what does this actually mean/do ?

  • @nicolaramshell1582
    @nicolaramshell1582 3 місяці тому

    Good video. Which nodes did you use to draw the path ?

  • @melon9089
    @melon9089 Рік тому +4

    the most useful tutorial!:):)

  • @zault
    @zault 7 місяців тому +1

    I tried to join the discord to ask a question, but the discord link was broken. I was having trouble setting the position target on input to the position of the mouse, how do you do that?

  • @36OLink1
    @36OLink1 8 місяців тому

    seems like I missed something. my enemy is not moving and I did everything just like you said. it shows the path, but isnt moving. Im using an area3d to find the player. heres my code:
    extends CharacterBody3D
    var speed = 20
    var accel = 10
    var player
    var gravity = 5
    @onready var nav :NavigationAgent3D = $NavigationAgent3D
    func _physics_process(delta):
    var direction = Vector3()
    if not is_on_floor():
    velocity.y -= gravity * delta
    else:
    pass
    if player:
    nav.target_position = player.global_position

    direction = nav.get_next_path_position() - global_position

    direction = direction.normalized()

    velocity = velocity.lerp(direction * speed, accel * delta)

    move_and_slide()
    func _on_area_3d_body_entered(body):
    if body.is_in_group("player"):
    player = body
    print("found")

  • @perplexusFructus
    @perplexusFructus 10 місяців тому +1

    Thanks a lot for your video. Now learning Godot looks a bit easier for me. :D

  • @1986DarkStalker
    @1986DarkStalker 8 місяців тому +1

    Hey, thanks for the quick tutorial. I have a lil problem tho: my knight dude isn’t walking to the destination, but floating through the air about as high as the navmesh. I tried modifying the origin for both the CharacterBody and the Agent, but he still refuses to use the actual ground.

    • @1986DarkStalker
      @1986DarkStalker 8 місяців тому +1

      Nvm, just had to change the cell height in the navmesh itself

  • @joschi1616
    @joschi1616 Рік тому +5

    Following this tutorial, my character is floating above the ground, as your character is. Is there a way to generate the navigationMesh so, that the character is actually on the ground and uses its collision shape for colliding with the ground?

    • @Narwhelington
      @Narwhelington Рік тому +1

      there is a setting in the NavigationAgent3d or region i forget node that fixes this i cant remeber what one it is but its like an offset or something

  • @black_forest_
    @black_forest_ 7 місяців тому

    Mh, I tried to apply the same principals to a navmesh baked from a GridMap. The Baking works fine but the collision seems to have some problems, such as the capsule going through the floor and such. Ill try to find a fix. I seem to be getting different results based on the y value of the Vector. I assumed that wouldnt matter. Does it always have to be a target on the ground? If so, how do you deal with slopes?

  • @abradotcs
    @abradotcs  Рік тому +3

    Another lazy tutorial where i barely explain anything XD Join my discord: discord.com/invite/qv8ECWuXxm

    • @tqssuki9632
      @tqssuki9632 4 місяці тому +1

      and you ask us to join your discord, even though you didn't explain the target? agh, you peach

    • @tqssuki9632
      @tqssuki9632 4 місяці тому

      What means that heart

    • @tqssuki9632
      @tqssuki9632 4 місяці тому

      Ok

    • @tqssuki9632
      @tqssuki9632 4 місяці тому

      Sorry for saying all those things, i just want to know if you will make another video explaining the target code of this video

  • @Matt-xi6tw
    @Matt-xi6tw 10 місяців тому +2

    How did you make the target for it to go to?

  • @cptfurball
    @cptfurball Рік тому +2

    Berry saves the day!~

  • @noone6422
    @noone6422 4 місяці тому

    thx for the video it helped me alot . but i cant quite wrap my head around how did you refrence the target in your script . i am quite new to 3d godot s

  • @rhevoramirez7969
    @rhevoramirez7969 8 місяців тому

    Thanks, my friend.

  • @BumpDev
    @BumpDev Рік тому +5

    Can you make one with navigation2d too

    • @abradotcs
      @abradotcs  Рік тому

      I will see what i can do :p

  • @DailyMemes-uz5er
    @DailyMemes-uz5er Рік тому +1

    Time to create nextbots in godot! Awesome tutorial.

    • @chongthechihuahua2643
      @chongthechihuahua2643 11 місяців тому

      bro you better give me the download file for your game, i always love games with nextbots!

  • @HanProgramer
    @HanProgramer 4 місяці тому

    Why is this marked as experimental? Only the agent is not. Kinda scared of using it as it might get removed

  • @Betegfos
    @Betegfos 7 місяців тому

    I think I missed where the target is declared in the script. Is it possible to make the character patrol between various points using the navigation agent?

  • @ajinkyax
    @ajinkyax Рік тому

    thanks Berry Alan. it worked :) Im building a retro monster game

  • @neptun3189
    @neptun3189 5 місяців тому

    5600+ notifications on the board 💀
    lol love your stuff dude

    • @abradotcs
      @abradotcs  5 місяців тому

      I think most of that was just fps print xD

  • @Jcundo
    @Jcundo 7 місяців тому

    Do a random movement tutorial pls

  • @chongthechihuahua2643
    @chongthechihuahua2643 11 місяців тому +1

    hey i tried to follow the code for this but i got these errors, "Line 11:Identifier "Global" not declared in the current scope.
    Line 16:Identifier "velocity" not declared in the current scope.
    Line 16:Identifier "velocity" not declared in the current scope.
    Line 18:Function "move_and_slide()" not found in base self." can u pls help me i really want to put this in my game

    • @MrBoingus
      @MrBoingus 10 місяців тому +2

      in this video, the creator of the video created his own custom autoload/global script called "Global". it does not exist in Godot by default, which is why you cant access it. basically what it does is contain the target location of the player in a variable that is globally accessible. i chose not to do this in my own project, and instead gave my player a "target" variable that is updated every physics tick by a script at the head of the scene. the head script uses the simple code:
      func _physics_process(delta):
      $Player.target = $Marker3D.position
      as you can see, i've made it so that the position of a Marker3D is set as the target location for the character to move to. so, instead of "nav.target_position = Global.target.position" like in the video, my code says "nav.target_position = target". it comes to the same conclusion but with different methods. you're welcome to do it any way you like.
      as for the other errors, it sounds to me like your player character isn't a CharacterBody3D. because of this, it doesnt have access to variables and functions contained in the CharacterBody3D class.

    • @DareChimera
      @DareChimera 10 місяців тому

      @@MrBoingusdoing that gives me an error code as well.

    • @MrBoingus
      @MrBoingus 10 місяців тому

      @@DareChimera that is not particularly helpful. what does your error say?

    • @tqssuki9632
      @tqssuki9632 4 місяці тому

      @@MrBoingus Unexpected "Identifier" in class body

  • @SensitiveSocietysBBL
    @SensitiveSocietysBBL 11 місяців тому +2

    how do you set the target

  • @averywyatt-luth1642
    @averywyatt-luth1642 5 місяців тому

    I tried to put this in a test and in the script viewer it comes back with "Identifier "nav" not declared in the current scope"
    I have the object that acts as the floor with falls set as it's own scene, would that change the process I need to take to get this to way?

  • @Robin_H.I
    @Robin_H.I Рік тому +10

    Hey i tried making the path finding towards the player then it did not worket it says on line 12 or "nav.target_position = global.target.global_position" that "identifier global not declared in the current scope" but i made the target an marker3D so if there id anything that you might know to be wrong or any way i could get some help. I would be very greatful (btw i have not used GODOT for a while so it might be obius) Hope you have an nice day and please excuse my bad english

    • @blinkapec
      @blinkapec Рік тому +2

      Same issue :(

    • @OnlySlightlyPsycoticHuman
      @OnlySlightlyPsycoticHuman Рік тому +9

      extends CharacterBody3D
      const speed = 5.0
      const accel = 10.0
      @onready var nav: NavigationAgent3D = $NavigationAgent3D
      @onready var Target = $"../Target"
      func _physics_process(delta):

      var direction = Vector3()

      nav.target_position = Target.global_position
      direction = nav.get_next_path_position() - global_position
      direction = direction.normalized()

      velocity = velocity.lerp(direction * SPEED , accel * delta)


      move_and_slide()

    • @OnlySlightlyPsycoticHuman
      @OnlySlightlyPsycoticHuman Рік тому +2

      there u go anyone with the same prob i figured out how 2 fix it

    • @flyingjudgement
      @flyingjudgement Рік тому +1

      Thanks! @@OnlySlightlyPsycoticHuman

    • @OnlySlightlyPsycoticHuman
      @OnlySlightlyPsycoticHuman Рік тому +1

      ur welcome@@flyingjudgement

  • @AndyDillbeck
    @AndyDillbeck 5 місяців тому

    how would you create the navigation mesh in code?
    I'm creating levels in runtime, and so I need to be able to set the navigation mesh when the level is created.

  • @rhevoramirez7969
    @rhevoramirez7969 10 місяців тому

    Thank you

  • @underatedstar
    @underatedstar Рік тому +1

    Questions, does this take terrain height into account? And can the mesh be rebaked/scanned on command in runtime?

  • @simmerdan
    @simmerdan 7 місяців тому

    Is it possible to limit movements? As if it were moving in a grid?

  • @youdi615
    @youdi615 Рік тому +2

    is it possible to make the ai move not so close to the wall?

    • @abradotcs
      @abradotcs  Рік тому +1

      Increase the agent radius

    • @youdi615
      @youdi615 Рік тому

      @@abradotcs thank you! but now i am having the issue where the agent is running into the obstacles, i have a static body on the objects and not sure why its doing that

  • @peter_shoes_
    @peter_shoes_ Рік тому +1

    Really excellent video! Just a heads up, Godot is pronounced "Ga-doh"

  • @apoorvpandey3D
    @apoorvpandey3D Рік тому

    how to add multiple targets so that the character can go on them 1 by 1

  • @bruceparker6142
    @bruceparker6142 Рік тому

    Is this what is used to steer AI cars in racing games?

  • @Waliente
    @Waliente 9 місяців тому

    the region settings wokrs also for 2d?

  • @JD-pi2ce
    @JD-pi2ce 11 місяців тому

    Sadly this doesn't work for me in 4.1.3. The node moves only to the first NavPoint and then stops.

  • @TheEshanDas
    @TheEshanDas Рік тому

    Hi, can you please explain how we can re-bake the navigation mesh automatically when an object is placed during runtime?

    • @abradotcs
      @abradotcs  Рік тому +2

      You just need to call bake_navigation_mesh on your NavigationRegion and it should rebake it. Becareful doe don't call it too often it could lower your fps significantly.

    • @TheEshanDas
      @TheEshanDas Рік тому

      @@abradotcs thank you for the reply. But my problem is that my map is quite big and thus baking takes time. Plus it has a city builder component, so the player might add new objects in the world pretty frequently. I am trying to build an RTS game actually and i am a complete noob with some programming background.
      So how do i suggest i handle my problem? Any tips will be much appreciated

    • @j0rp
      @j0rp 9 місяців тому

      @@TheEshanDas The navigation agent has a avoidance function toggle (defaults off). Then it will avoid any navigation into a NavigationObstacle node, which can be added to any model/body in the game similar to a collision mesh. Here's an example of a 2D nav agent with avoidance: ua-cam.com/video/5bv-IOF48hM/v-deo.html

  • @slimebuck
    @slimebuck Рік тому

    subbed, liked, an now im commenting

  • @zunden2
    @zunden2 3 місяці тому

    how to make the character facing target?

  • @StrongestEngineer
    @StrongestEngineer Рік тому

    Tyty great vid

  • @gergotancos1508
    @gergotancos1508 5 місяців тому

    How can i make the ai to avoid walls becouse its just keeps getting stuck on each wall that he faces.

    • @orwinsen4510
      @orwinsen4510 4 місяці тому

      thats the point of the video bro..

  • @Hey-ur7bl
    @Hey-ur7bl 7 місяців тому +1

    whats the target script?

  • @bobsonssauro
    @bobsonssauro Рік тому +3

    Where are the source code?
    What is the script in the target?

  • @gamerbray.gt4
    @gamerbray.gt4 Рік тому

    can someone help? it give error Global is not in are current scope

  • @just_a_zombie6718
    @just_a_zombie6718 10 місяців тому

    Again, I love you!! wahahhahahaha

  • @Dodellal
    @Dodellal Рік тому

    Не работает твой код. Переделывай!

  • @ludo9311
    @ludo9311 5 місяців тому

    invitation discord not working @abra

  • @Boildroid
    @Boildroid Рік тому +3

    Cute Berry. 😉

  • @s3nju279
    @s3nju279 11 місяців тому +2

    error: NavigationServer map query failed because it was made before first map synchronization.

    • @graphtet3751
      @graphtet3751 10 місяців тому

      I am getting the same thing, has a solution been found

    • @s3nju279
      @s3nju279 10 місяців тому

      @@graphtet3751 i put the set navagent target position in an if statement. with a timer. so it waits a second. lets the navmesh do all its shit. i dont constantly update the nav agent position either i only update every couple seconds.

    • @Fletcher64
      @Fletcher64 10 місяців тому +3

      You need to await get_tree().physics_frame before you start referencing navigation because the nav server doesn't start for a frame after the game

    • @Avonaeon
      @Avonaeon 8 місяців тому

      @@Fletcher64 Thank you so much! Been struggling with this and this solved it!

    • @da_cat
      @da_cat 8 місяців тому

      this is more important than all the fucking trash tutorials i watched the entire day@@Fletcher64

  • @august0490
    @august0490 Рік тому +1

    Hi, what if an obstacle is added while you are playing? For example: a car move in front of the character.
    Is there another tool for dynamic objects?

    • @abradotcs
      @abradotcs  Рік тому +5

      You can rebake the navmesh in runtime!

    • @abradotcs
      @abradotcs  Рік тому

      There is also navigation obstacles but i don't recommend using them

    • @august0490
      @august0490 Рік тому

      @@abradotcs thanks! do you have a tutorial about it?