#Navigating Around Obstacles with NavigationAgent2D and NavigationObstacle2D

5 messages · Page 1 of 1 (latest)

bronze adder
#

I'm encountering difficulties with implementing navigation functionality in my project and haven't been successful yet. I would greatly appreciate any guidance or advice you could provide.

Here's an overview of the setup I have:

  • Wall: This is a static element in my scene, composed of StaticBody2D, CollisionShape2D, and NavigationObstacle2D components. It's meant to serve as an obstacle.
  • Enemy: This dynamic entity is made up of Area2D, CollisionShape2D, and NavigationAgent2D components. In my main scene, I have one instance of the Wall, one Enemy, and a NavigationRegion2D to manage navigation.

My objective is to make the Enemy move towards the position of my mouse cursor while automatically navigating around the Wall, rather than attempting to move through it. I decided to use Area2D for the Enemy due to considerations for performance, even though I'm aware that Area2D does not inherently possess a velocity vector. Thus, I move it by directly updating its position.

Here's the approach I've taken so far in the script attached to the Enemy (enemy.gd):

extends Area2D

@onready var nav: NavigationAgent2D = $NavigationAgent2D
const SPEED = 300

func _physics_process(delta):
    if Engine.get_process_frames() % 10 == 0:
        nav.target_position = get_global_mouse_position()
    
    var direction = (nav.get_next_path_position() - global_position).normalized()
    
    position += direction * delta * SPEED

Despite this setup, the Enemy doesn't navigate around the Wall as intended, instead, it appears to ignore the obstacle altogether (based on a screenshot).

Could you suggest what adjustments I need to make to ensure that the Enemy navigates around the Wall, taking into account the navigation path properly?

🙏

bronze adder
#

Ok. Now I understand that NavigationObstacle2D should be in the NavigationRegion2D's tree to be baked. Is it possible to bypass this requirement and mention my obstacles into one global NavigationRegion2D somehow not with the tree?

bright ravine
#

Hi there 😊,

This is possible, but somewhat more complicated. Take a look at the example script the documentation provides.

https://docs.godotengine.org/en/stable/tutorials/navigation/navigation_using_navigationmeshes.html#navigation-mesh-script-templates

#

I would recommend using the default behavior and order your scene tree accordingly.

frosty jungle
#

you can bake nodes by group name when you switch the settings on the NavigationPolygon for the baking. That way the nodes can be anywhere as long as they are in the SceneTree.