Elif raycast does not not dectect player not working

Godot Version



I am trying to make a raycast that if it dectect the player it will chase it and it works but i want to make a else if does not dectect it then it won’t chase and it didn’t work and now it does not chase the player if it dectect’s it. here the code:
extends RayCast2D

@export var enemy: Enemy
@export var abr = 5
var angle_cone_of_vision := deg_to_rad(360)
@export var range = 0.0
var max_view_distance = range * 10

Called when the node enters the scene tree for the first time.

func _ready():

max_view_distance = range * 10

func generate_raycasts() → void:
var angle_between_rays := deg_to_rad(abr)
var ray_count := angle_cone_of_vision / angle_between_rays

for index in ray_count:
	var ray := RayCast2D.new()
	var angle := angle_between_rays * (index - ray_count / 2.0)
	ray.target_position = Vector2.UP.rotated(angle) * max_view_distance
	ray.enabled = true

func _physics_process(delta: float) → void:
for ray in get_children():
if ray.is_colliding() and ray.get_collider() is Player:

		enemy.player_in = 1
		enemy.marker_finder = 0
		enemy.player_in = 0
		enemy.marker_finder = 1

here the picture of the raycast:

i think the problem now there are raycast those give different set value of player_in and marker_finder for the enemy
hence why the chase stopped
you can try print something in if ray.is_colliding() and ray.get_collider() is Player: and the else:. there should be many print executed in else because some of the raycasts are not colliding any right now while some did

also the picture shows kinda weird raycast projection, usually when raycast collide, it will shorten to the collision, not like the picture shown with the raycast failed to be stopped in front of the player

Is there anyway that I can fix it?

first get your raycast actually hit the player first, you will need your player to be having PhysicsBody2D, simply make the player to be CharacterBody2D.