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.

in Engine by (32 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.

by (3,249 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)

What do I put under the if statement?

Ras0922$$ already had an area signal with that functionality in it, so it depends on how your game's set up, here's a very simple example using look_at.

if raycast.is_colliding() && raycast.get_collider() == player:
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.