RayCast2D Collider Stuck On TileMap

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By srcarr1515

I’m currently trying to implement a RayCast2D so my enemies can have a line of sight and chase the player when they can “see” him unobstructed. It works initially, however when I run from the enemy for a little while, the raycast gets stuck on the Tilemap (even when it’s no longer overlapping tiles that have a collision mask)

I’ve tried flipping the collision bit on and off, I’ve tried add/remove child, I’ve also tried add the tilemap as an exception (and then later clearing exceptions) but nothing seems to work.

Has anyone ever ran into this?

I’m using Godot 3.5.1

Here is the code involved in this issue:

onready var hitbox := $HitBox
onready var detectbox := $DetectBox
onready var hurtbox := $HurtBox
onready var fsm := $StateMachine
onready var los = $LineOfSight ## <<-- This is the Raycast2D
onready var attention_timer = $AttentionTimer
var los_instance

export var line_of_sight_range := 120
var target

func _ready():
	nav_agent.connect("velocity_computed", self, "_on_velocity_computed")

func _on_velocity_computed(velocity):
	pass

func _physics_process(delta):
	look_for_player()
	if !target:
		fsm.change_to("Idle")
		

func look_for_player():
	if GameData.player:
		var los_range = min(los.global_position.distance_to(GameData.player.global_position), line_of_sight_range)
		los.cast_to = (los.global_position.direction_to(GameData.player.global_position) * los_range)
		los.force_raycast_update()
		if los.is_colliding():
			if los.get_collider().is_in_group("player"):
				target = los.get_collider()
				fsm.change_to("Chase")
				attention_timer.start()
				

func _on_NavTimer_timeout():
	if GameData.player.global_position:
		nav_agent.set_target_location(GameData.player.global_position)