I am currently making a top down shooter and want my enemy to not see through walls.
I am using Area2D to detect the player. The walls are static body.
You can use a Raycast2D, use get_collider() to get the first object it hits and check if that object is the player.
But Raycast2D is for one direction only while using Area2D the enemy is able to look 360 degree.So I used Area2D.
In that case use both. Detect the player with the area then set the raycast cast_to to the player's position, that will then detect if there's a wall in the way.
can you give me an example of the code?
raycast.cast_to = player.global_postion-raycast.global_position #cast_to is local to the raycast node
raycast.enabled = true
if raycast.is_colliding() && raycast.get_collider() == player:
#can see player
Raycast is returning false for is colliding even though cast to is at player position(local to Enemy/RayCast)