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?
🙏