Avoidance Problem - Navigation Agent 2d

Godot Version

4.3

Question

I followed this tutorial for the movement of my enemies with AI and it works very well in general the problem is that the player crosses the obstacles, I am doing everything the same as in the tutorial, the use of the tile maps, the scripts and the configuration

Problem:

Code:

extends CharacterBody2D

@export var  walk_speed = 50
@export var player : CharacterBody2D

@onready var navigation_agent = $NavigationAgent2D

func _ready():
	set_physics_process(false)
	await get_tree().create_timer(.25).timeout
	set_physics_process(true)

func _physics_process(delta: float) -> void:
	#navigation_agent.target_position = get_global_mouse_position()
	navigation_agent.target_position = player.global_position
	
	var current_agent_position = global_position
	var next_path_position = navigation_agent.get_next_path_position()
	var new_velocity = current_agent_position.direction_to(next_path_position) * walk_speed
	
	if navigation_agent.is_navigation_finished():
		return
	
	if navigation_agent.avoidance_enabled:
		navigation_agent.set_velocity(new_velocity)
	else:
		_on_navigation_agent_2d_velocity_computed(new_velocity)
	
	move_and_slide()

func _on_navigation_agent_2d_velocity_computed(safe_velocity: Vector2) -> void:
	velocity = safe_velocity

I can’t help you with this but if you add the code and either use the format code button or put 3 backticks above and below it (```) other’s can try to help easier because they can try to find the problem there :smiley:

1 Like

thanks for the suggestion!!

I found the solution for this problem