make an agro zone with obstacles

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Timofey

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.

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

exuin | 2021-10-09 15:17

:bust_in_silhouette: Reply From: Tato64

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