Godot Version
4.4.1
Question
Hey guys, my pathfinding AI cant go around walls, yet i’ve tried for several hours to fix this. I have tried all the basic, simple, and some of the complex methods. (mostly everything). if somebody knows the secret workaround, please tell me. Heres the full glitch explained: The agent calculates a path around the wall, yet they still go towards the wall and slide on the wall. The code:
extends CharacterBody3D
#@onready var player = get_tree().get_first_node_in_group(“Player”)
#@onready var agent = $NavigationAgent3D
#@onready var ray = $RayCast3D
#var speed := 4.0
#func _physics_process(delta):
#print(agent.is_navigation_finished())
#if can_see_player():
#print("Saw player")
#agent.target_position = player.global_position
#var next = player.global_position
#print(global_position.distance_to(next))
#var dir = next - global_position
#dir.y = 0 # Ignore up/down
#
##velocity = (next - global_position).normalized() * speed
##var dir = next - global_position
#
#look_at(global_position * 2 - player.global_position, Vector3.UP)
#dir.y = 0
#velocity.x = dir.normalized().x * speed
#velocity.z = dir.normalized().z * speed
#move_and_slide()
#
#print("Frog:", global_position)
#print("Player:", player.global_position)
#print("Next:", agent.get_next_path_position())
#move_and_slide()
#func can_see_player():
#ray.target_position = to_local(player.global_position)
#ray.force_raycast_update()
#if !ray.is_colliding():
#return true
#return ray.get_collider() == player
#func _ready() → void:
#$AnimationPlayer.play(“Animation”)
@onready var player = get_tree().get_first_node_in_group(“Player”)
@onready var agent = $NavigationAgent3D
@onready var ray = $RayCast3D
var next
@onready var patrol_markers: Array[Marker3D] = [%Marker3D, %Marker3D2, %Marker3D3, %Marker3D4, %Marker3D5, %Marker3D6, %Marker3D7, %Marker3D8]
@onready var marker_1 = %Marker3D
@onready var marker_2 = %Marker3D2
@onready var marker_3 = %Marker3D3
@onready var marker_4 = %Marker3D4
@onready var marker_5 = %Marker3D5
@onready var marker_6 = %Marker3D6
@onready var marker_7 = %Marker3D7
@onready var marker_8 = %Marker3D8
@onready var vase = %Vase_Real
#var distance = global_position.distance_to(patrol_markers[current_index].global_position)
var speed := 6.8
var current_index := 0
var hovering = false
func _physics_process(delta):
print(agent.is_navigation_finished())
#if can_see_player():
#agent.target_position = player.global_position
#elif vase.dropped_true_or_false:
#agent.target_position = vase.global_position
#else:
#if not patrol_markers.is_empty():
#agent.target_position = patrol_markers[current_index].global_position
# Priority check
#var new_target = global_position
#if can_see_player() and is_instance_valid(player):
#new_target = player.global_position
#elif is_instance_valid(vase) and vase.dropped_true_or_false:
#new_target = vase.global_position
#elif not patrol_markers.is_empty():
#new_target = patrol_markers[current_index].global_position
#if not patrol_markers.is_empty():
#next = patrol_markers[current_index].global_position #- VERY IMPORTANT KEEP
#if next.distance_to(Vector3i(0, 0, 0.5)):
#current_index = (current_index + 1) % patrol_markers.size() # loop back to 0 after the last one
#next = patrol_markers[current_index].global_position
#agent.target_position = next
#print("Current IDX: ",current_index)
#if distance < 10.0:
#current_index = (current_index + 1) % 8
if not patrol_markers.is_empty():
# Keep the current target
next = patrol_markers[current_index].global_position
agent.target_position = next
# Only move to the next marker WHEN we have actually arrived
if agent.is_navigation_finished():
current_index = (current_index + 1) % patrol_markers.size()
next = patrol_markers[current_index].global_position
print("Current IDX: ", current_index)
var next_step = agent.get_next_path_position()
var current_pos = global_position
#sprint(global_position.distance_to(next))
var dir = (next_step - global_position).normalized()
dir.y = 0 # Ignore up/down
var map = agent.get_navigation_map()
agent.get_next_path_position()
print(agent.get_current_navigation_path())
print("Can reach IDX ", current_index, "? ", agent.is_target_reachable())
if vase.dropped_true_or_false == true:
next = vase.position
velocity = (next - global_position).normalized() * speed
#var dir = next - global_position
#look_at(global_position * 2 - player.global_position, Vector3.UP)
#velocity.x = dir.x * speed
#velocity.z = dir.z * speed
if agent.is_navigation_finished():
print("Arrived at the waypoint! Time to pick a new one.")
print("Frog:", global_position)
#print("Player:", player.global_position)
print("Next:", agent.get_next_path_position())
#agent.set_velocity(velocity)
move_and_slide()
if player.inventory.has("Gun") and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and hovering == true:
$AnimationPlayer.play("Dsdie")
print("Shot")
await get_tree().create_timer(30.0).timeout
print("froggo is back")
func can_see_player():
#ray.target_position = to_local(player.global_position)
ray.force_raycast_update()
if !ray.is_colliding():
return true
return ray.get_collider() == player
func _ready() → void:
$AnimationPlayer.play(“Animation”)
agent.target_reached.connect(_on_navigation_agent_3d_target_reached)
agent.target_position = marker_1.global_position
current_index = 0
func _on_target_reached():
pass
func _on_navigation_agent_3d_target_reached() → void:
#if can_see_player():
#return # Stay on player if chasing
#if patrol_markers.is_empty():
#return
## Move to the next marker in the array automatically
#current_index = (current_index + 1) % patrol_markers.size()
#agent.target_position = patrol_markers[current_index].global_position
print("Reached target! Next marker index: ", current_index)
func _on_death_box_mouse_entered() → void:
hovering = true
func _on_death_box_mouse_exited() → void:
hovering = false