• Godot Help
  • How do I make a RayCast2D cast to an enemy?

Hi,

I have an area around my player to detect invisible (hidden) bodies(enemies).
Once they enter the area of the player, the enemy becomes visible.
But I dont want them to be detected through walls.
So I wanted to cast a RayCast2D to the body entering the area to check for collision.
The problem is, that the RayCast just never points at the body (enemy). I tried many different methods and it always shows in different directions than the entered body.

The code:

func _on_Area_body_entered(body):
if body.is_in_group("enemies"):
    var direction_to_enemy = raycast.global_position.direction_to(body.global_position)
    raycast.cast_to = direction_to_enemy * 400
    raycast.force_raycast_update()
    var collider = raycast.get_collider()
    if collider == body:
        body.show()
    else:
        pass

The scene structure:

Node2D(world)
-YSort
--Enemies
-Player
--RayCast2D

Is player node rotated?

Have you tried using print to see if the raycast is even activating and picking anything up?