why my mob raycast only detect my player, when my player at top of mob?

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

for some reason my mob sight only become true when my player at top him and false when have something in between player and mob. if go to left/right/bottom mob, i did’t get any print , but if put something in between i will get false print. did i do right?

mob code

onready var player = get_parent().get_node("player")
var player_in_range
var player_in_sight

func _physics_process(delta: float) -> void:
func _on_sight_body_entered(body: Node) -> void:
	if body == player:
		player_in_range = true
		print(self.name," player in range ", player_in_range)

func _on_sight_body_exited(body: Node) -> void:
	if body == player:
		player_in_range = false
		print(self.name ," player out of range ", player_in_range)

func sight_update():
	if player_in_range == true:
		var space_state = get_world_2d().direct_space_state
		var sight_check = space_state.intersect_ray(position, player.position, [self], collision_mask)
		if sight_check:
			if sight_check.collider.name == "player":
				player_in_sight = true
				$icon.modulate = ColorN("black",1)
				print(self.name," player in sight = ", player_in_sight, " = ",sight_check.position)
				player_in_sight = false
				$icon.modulate = ColorN("crimson",1)
				print(self.name," player not in sight = ", player_in_sight, " = ",sight_check.position)

map node
enter image description here

:bust_in_silhouette: Reply From: timothybrentwood

Unless you want to have obstacles/other targets to block abilities, I would probably use an Area2D node to check whether your player is in range of an enemy. The Raycast.get_collider() function returns only the first object that it collides with.

With an Area2D node you can use the get_overlapping_bodies() function to get all bodies within your a certain distance of your enemy then check to see if one of them is your player. Or set the collision bits in such a way that enemies can only detect players and vise versa.