0 votes

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.

asked Jul 17 in Engine by Ras0922$$ (30 points)

1 Answer

0 votes

You can use a Raycast2D, use get_collider() to get the first object it hits and check if that object is the player.

answered Jul 17 by Magso (2,851 points)

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?

Like this

func on_area_body_entered(body):
    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)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.