Problem with AI pathfinding when paired with ray cast 2d

using godot 4.3

I’m creating an enemy ranged AI in a top-down rougelite, and since I wanted to make sure that my enemy had line of sight before firing (to avoid randomly firing at the wall when it was in desired distance) I added a Raycast 2D and some line-of-sight-code, but when I hide behind a wall that has a corner to it the enemy stops following the pathfind and randomly jetters in place.

Here’s the code I have:

extends CharacterBody2D

const SPEED : float = 40

@export var target = CharacterBody2D
@onready var ray_cast_2d: RayCast2D = $RayCast2D
@onready var navigation_agent_2d: NavigationAgent2D = $NavigationAgent2D

func _ready() → void:
call_deferred(“SeekerSetup”)

func SeekerSetup():
await get_tree().physics_frame
if target:
navigation_agent_2d.target_position = target.global_position
func _physics_process(delta: float) → void:
ray_cast_2d.target_position = target.global_position - self.global_position
ray_cast_2d.force_raycast_update()
if target:
navigation_agent_2d.target_position = target.global_position
if navigation_agent_2d.is_navigation_finished() and has_line_of_sight():
return
var CurrentAgentPosition = global_position
var NextPathPosition = navigation_agent_2d.get_next_path_position()
velocity = CurrentAgentPosition.direction_to(NextPathPosition) * SPEED
move_and_slide()

func has_line_of_sight():
if navigation_agent_2d.is_target_reachable() == false:
print(“fuck”)
get_tree().quit()
if ray_cast_2d.is_colliding():
print(“is colliding with” + str(ray_cast_2d.get_collider()))
if ray_cast_2d.get_collider() == target:
return true
else:
return false

I can’t figure out how to fix it, and it’d be really nice to have my ai not buzzing around like a baby on coffee. here’s a screen shot of the glitch

the enemy is the icon in the top right corner. if you could help that’d be great!