–3 votes

Make an agro zone, but taking into account the obstacles behind which the player will not be visible. I had an idea to do this using raycast2d, but I think to put them in a circle would not be rational.

Godot version 3.3.2.stable.official
in Engine by (159 points)

What's wrong with using RayCast2D? I think it would work.

1 Answer

0 votes

You will first need an area (with a circular shape, probably) and a raycast.

On the area, use the signal "On body entered"

Make some kind of check to see if the body, a simple one would be putting the player node in a specific physics layer, and put that layer as the "mask" the area can check for. (Google "Godot layers and masks") THIS IS ONLY FOR THE AREA, THE RAYCAST NEEDS TO BE ABLE TO "SEE" MORE MASK LAYERS.

When the player body node enters the area, make the raycast look at the player node, for example: $Raycast2D.look_at(body.global_position)

Then check if the raycast is colliding with the player, this means there is direct line of sight, for example, im gonna use the player node's name for this one:

if $Raycast2D.is_colliding():
     if $Raycast2D.get_collider().name == "Player":
          do stuff
by (309 points)
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.