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
@@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?
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 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).
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.
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
@@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()
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.
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:
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?
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"
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.
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?
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.
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.
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..
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.
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.
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
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.
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 :)
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?
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.
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.
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
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.
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)
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.
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
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
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
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?
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?!?!?!
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 ;)
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()
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
Thanks drobison100
Thanks drobison100
Thanks drobison100
Thanks drobinson100
This might explain my Getting a non_existent_function for 'get_next_location() function call
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()
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)"
@@iBurntMyCerealAgain omg, u a my hero
@@iBurntMyCerealAgain Thank you so much.
Thank you
@@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?
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.
Happy you were able to find it useful! Thanks for watching. :)
same problem i'm facing now
Nice example. Switching from Unity to Godot so these tidbits really help.
Thanks! Let me know if there’s any tutorials/examples in particular you could use. Looking for more ideas on what to make.
@@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).
@@DevLogLogan Advanced Ai is onenof those ideas.
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.
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
Thank you! This also works in version: Godot_v4.2.1-stable
You're the best, thanks. 👍
@@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()
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.
HOW?, IT TAKES ME LIKE TWO DAYS TO IMPLEMENT A FEAUTRE LIKE THIS
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:
Thanks! That was easier than expected. Momentum was a nice touch too.
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
Wow how simple it in Godot 4. But it wouldn’t so good if you don’t explain so clearly. Thanks you so much 🙂
Slava Ukraini!
@@DamnMascus Героям слава!
amazing tutorial, thanx for sharing! the enemies still move in like a triangle formation. looks hilarious :D
This is actually just perfect, thank you very much
thanks for the help! Helped a lot
Used this video to get avoidance working in my game. Thank you!
The move_toward solution is what I needed to solve a problem I have had for ages, thank you for the suggestion
Perfect and concise intro to this functionality. Thanks!
Brilliant. This video really helped me out. Keep it up!
Thank you! You just saved a lot of hassle for me when it comes to pathfinding
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?
This helped a lot thanks
Thank you for the tutorial and the explanations it was really helpful!
Does anyone know what the environment is? I can't find in Godot 4
Wouldn't it be great if we could draw or manually edit our navmeshes?
thank you! nice job
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"
so it's that easy, huh?
great video, very quick to show how to make it work
so my enemy just moves to the centre of the map, instead of following the player?
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.
var rotation_speed = 20
$MeshInstance3D.rotation.y = lerp_angle($MeshInstance3D.rotation.y, atan2(-velocity.x, -velocity.z), delta * rotation_speed )
Works good :)
I’m just using a sprite3d with billboard mode activated.
great tutorial
Crazy good tutorial! Thank you!
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?
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
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.
Your videos are f#cking amazing.
How did you get the enviroment node? Has it been renamed?
yea i am thinking the same lol btw in the beginning of the world you made a node so i think u should attached it to it
Its called CSGCombiner3D
@@gondre7975 ah ok
@@gondre7975 thx
Great! Thankyou! :)
Thank you so much dude, you've helped me a lot!
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.
0:00 What node is the "environment?"
I have no idea what I'm doing wrong but my screen just goes black or grey when I test with the added enemy.
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..
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.
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.
Exact same problem. Make sure that the CharacterBody3D is not a Child Of any other nodes. Fixed it for me.
@@basscrowdead I had fixed it, and thats the way i did it. thank you very much for the help!
Thank you so much :D
Thank you very much!!!
Thank you!
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
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.
@@user-xsn5ozskwg OK thank you
thanks you. can you make more of these videos sir?
Great tutorial! Can you do a tutorial for voxel terrain?
too bad look like it not working in stable 4.0
Invalid call. Nonexistent function 'get_next_location' in base 'NavigationAgent3D'.
it changed to get_next_path_position()
my enemy gets stucked after moving to a certain positon
my enivironment is simple plane
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
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 :)
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?
seeing all the other comments asking the same question
guess ill just find another tutorial
My enemies are not moving, in code its says it moves but it dosent. What can i do?
How can you tell the code is telling the enemies to move? Is it possible you're missing the move_and_slide method?
Im having a nonexistent function problem with 'set_target_location'
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
What icon is that environment node? I dont see anything like that in 4.0.2
In case you haven't found it yet, it's a CSGCombiner3D
@@monet5889 thank you!
anyone know how to make the enemies rotate towards you
For some reason it says delta is never used in the function which sounds like it doesn’t make any sense
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.
i get this error:
Invalid operands 'Vector3' and 'float' in operator '+'.
anyone know how to help?
you can't add a Vector3() to a float.
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.
Thanks, I didnt know what was going on...
I was experiencing an error on var new_velocity = (next_location - current_location).normalize() * speed
I dont know how fix it
.normalized() dont forget the 'd'
Great Tutorial! You would make it more easy if you gave access to the test world files :)
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
Nevermind lol! Turns out it was the "Agent Height Offset" that was the problem
What about rotation?
Yo dude the NavigationAgent3d node is gone in my current version. Is it now called something else
what version is it? Even on the newest version (godot 4.2) the NavigationAgent3D node is called that
@@Bluepikminproductions Godot 4.0
its good and all and I appreciate the tutorial however how am I supposed to rotate the enemy towards the player
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.
@@mattvonfat Thank you I'll try it and see if it works , regardless if did or not I appreciate your reply fam :)
My enemy just yeets himself off the map and continues to go forever im at a loss. Can anyone help?
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)
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.
I LOVE YOU 💞💞💞💞💞💞💞💞💞💞💞💞💞💞
doesnt work :(
Joka = 🌳
aw hell naw
anyone know what they changed _physics_process(delta) to?
That method still has the same name, what issues are you having?
in my project bluds just standing there not moving
were you able to solve it
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
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
fixed it, I wasn't referencing the node properly.
@@memze1148 help me out ...please
I kepp getting the _physics_process() "delta" error
Very nice video, but change that keyboard 😅
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)
Can you bake the NavMesh in script? For like, a procedural terrain.
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?
nav_agent.get_next_path_position()
Position, not location!
@@DAYDAYBEATZ Thank you!
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?!?!?!
Godot 4.1 release made some changes to NavAgent , look at first comment to fix the
uh, u used mesh instance? or just the script is in it?
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 ;)
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()
Having same issue😢
SO USEFULL THANKS
Thank you so much mate!
Does anyone know how to make a ai where if it's in range it will chase otherwise it will look around
THANK YOU SO MUCH