How to use NavigationObstacle2D

Godot Version

v4.2.2.stable.mono.official [15073afe3]

Question

I’m trying to use the NavigationObstacle2D in my project. The NavigationObstacle2D do block the agent from walking into that zone but the agent don’t walk around it.

(I know I can cut a hole in the mesh but this is a test for eventually make moving obstacle. Moreover, I’ve all the tutorial and guide I could find about this. If you see a solution on the internet I’ve may have missed, please let me know)

Here’s the code for I’m using for navigation and the scene structure:

using Godot;

public partial class Enemy : CharacterBody2D
{
    public static Player player;
    [Export] NavigationAgent2D NavigationAgent;
    public override void _Process(double delta) {
        NavigationAgent.TargetPosition = player.GlobalPosition;
        if (NavigationAgent.IsNavigationFinished()) return;
        NavigationAgent.Velocity = (NavigationAgent.GetNextPathPosition() - GlobalPosition).Normalized() * 1000;
    }
    public void OnVelocityComputed(Vector2 vector) {
        Velocity = vector;
        MoveAndSlide();
    }
}

2024-07-08 19_00_25-(_) TestLevel.tscn - 2D_BasicShooterGame - Godot Engine

Have you activated the avoidance flag on the the navigationAgent2d?

Yes, I’ve checked avoidance_enabled to true. avoidance_layers and avoidance_mask in NavigationAgent2D and NavigationRegion2D both have the same bit mask as well.

Have you played around with the radius settings to see if theres enough space to walk around?

I’ve tried every setting I see in the inspector, as I’m struggle to fix this problem and communicate at delayed interval like this is unideal. Here’s my minimum project that reproduce the problem. Hopefully you can find some time and help me out.

(You can simply import it and it (should) open for you)
(The name is from a old debug project I had, hope you don’t mind)
https://drive.google.com/drive/folders/19dAx925KqQoR0hSHVbqFFHE1UoWgqSl8

I tried my own project and well when you reach an avoidance obstacle the path doesnt get recalculated, instead you are moving in somewhat random directions, hoping the obstacle will get out of the way

Yes, I did notice the path doesn’t change whether the obstacle is there or not. Is there something I’m suppose to do that I didn’t?

Im afraid thats how its supposed to be. You would have to calculate your own avoidance path if you want different behaviour. Avoidance should only be used when the object is moving a lot so baking the navmesh would be to expensive. If your obstacle is moving then the current implementation shouldnt be too bad

But in my project, the agent still go the same path whether the obstacle is there or not. Shouldn’t the agent go around if an obstacle is on the way? I saw some tutorial on Youtube and their implementation have the agent always avoid the obstacle mesh.

The NavigationObstacle is avoidance velocity only, it does nothing for the pathfinding that is affected by navigation mesh surface only.

See obstacle documentation that also mentions this in the first section. Using NavigationObstacles — Godot Engine (4.2) documentation in English

Note that this got changed in Godot 4.3 and the obstacle has an option to bake to the navigation mesh in that version. You still need to rebake your navigation mesh regardless.

Thank you for your explaintion, I unfortunately don’t quite get what it mean. Can you explain it to me?

You need to use the computed_velocity signal of the navigationagent2d and use move_and_slide (or whatever function you use) with the given velocity

But I did it in the debug project I sent. This is so weird…

Well i set it up from scratch and it worked. I wont download your files for security reasons but you can post pictures of how you move your character

Hello, here’s the video demonstration, the yellow zone is the obstacle mesh: https://drive.google.com/file/d/1iwqUTy15wjDmCNY4c5LHrGrma7PeFXWO/view?usp=drive_link

This tutorial explains it pretty good:

I followed it and it doesn’t work :frowning:

This has to be on your end then, since this is pretty clear and obviously works

I understand. This is probably an issue exclusively on my end. Thank you for spending time and patience on my issue.

I just looked in your original message and you set the velocity in your process function. Try to implement what the guy implemented in the video, then it should work