||Topic was automatically imported from the old Question2Answer platform.
Hi, I was wondering how to use the navigation agent. (Godot 4.0 alpha 7)
set_target_location ( Vector2 location ) is how the target location is done. But how is starting location handled.
get_nav_path ( ) keeps giving me an empty array.
I have a tile map with navigation and collision.
The documentation is not being very helpful here. It says:-
The agent needs navigation data to work correctly. NavigationAgent2D is physics safe.
navigation data means what. I thought the tile map with navigation tiles counted as navigation data.
The starting position is the agent’s parent position. Also
get_nav_path is empty because it is computed asynchronously. Wait for the
path_changed signal, after which
get_nav_path() should give a valid result.
I built a minimum example to test this. My hierarchy looks like this:
- nav_test (
- tile_map (
- player (
- agent (
- goal (
And the script attached to
@onready var agent: NavigationAgent2D = $player/agent
@onready var goal: Position2D = $goal
@onready var player = $player
func _ready() -> void:
func _physics_process(delta: float) -> void:
var next_location = agent.get_next_location()
var v = (next_location - player.global_position).normalized()
func on_velocity_computed(safe_velocity: Vector2) -> void:
player.position += safe_velocity
func on_path_changed() -> void:
func on_target_reached() -> void:
Thank you! I will test it out. And let you know.
Skydome | 2022-05-13 18:23
And btw how is collision avoidance handled with navigation agent? It says automatic but right now it gets stuck in walls.
Skydome | 2022-05-14 11:22
You need to define where it’s safe for an agent to travel. You can do this by adding
NavigationRegion2D nodes and drawing paths, or by using a
TileMap with navigation shapes defined on the tiles.
In either case you draw the paths where there is open floor, though be sure to leave a large enough gap between the walls and floor to account for the agent’s size.
Thank you again!
I am going to work with the answer you gave to my other question.
Dynamic obstacle avoidance is what I would like to do. Avoid NPCs that are not the target.
Skydome | 2022-05-15 20:55
To make a dynamic obstacle you would add a
NavigationObstacle2D node as a child to an object (npc, moving hazard, etc).
As long as you use the “safe” velocity from the
velocity_computed signal everything should work as expected.