I am using RayCastCallback to detect the line of sight. So I the enemy avoid seeing the player through walls, problem is there's a limited field of view. So I added another two rays to the left and right side, with a total of 3 rays. Another problem is when the distance is longer the rays can't handle to see the player, so I added another more rays to fill the field of view. I don't know if this kind of idea is good or there is a better way without using rays?

Look in the below image, it seems the there is a circular sensor or like a radar. I am trying to achieve this kind of enemy sight (enemy sight demo ).

enter image description here


2 Answers 2


You only need to do two things: check if there is line of sight (raycast between the player and enemy) and if the angle of that ray is within the field of view (using atan2 to get an angle or using the dot product between the facing vector and the raycast direction vector)

  • 1
    \$\begingroup\$ Why is this downvoted? The enemy sight issue is best solved by checking the line between player and enemy instead if casting a bunch of rays. \$\endgroup\$
    – Felsir
    Commented Sep 13, 2016 at 8:44

I found a solution with the help of box2d, I could create a circle or cone shape as FOV (Field of View) and set as sensor. Now that I could check if the enemy is on the FOV, I could now use a single ray to use in RayCastCallback starting from an enemy position to target position. In this kind of way I could now make the enemy avoid seeing through walls. I have not yet tested this but I think this would work and also I minimize the stress of using multiple rays.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .