Create a cone of enemy vision

taking as a reference the example of artificial intelligence in gdevelop:
I am attaching screen shot events for reference

I was wondering if there was a method to create a cone vision of enemies using raycast?
this is the result I would like to achieve

I have already tried with creating an object renamed cone vison where it changed position according to the direction of the enemy, but in my game waves of enemies are created and I think this affects the performance of the game too much.
I would like to understand if through the raycast it was possible to create a vision cone.
other things, in my game the enemies don’t rotate like in the example above

Hi, each raycast represents just a line. If you want to make a cone you would need to add several raycast-objects: two for the outline of the cone and several within the cone (depending on the size of your enemies). That’s definitely possible. An alternative to the raycast-objects would be a hidden, cone-shaped sprite object.

hello, I have already tried with the creation of the hidden vision cone with the sprite but I think it is too expensive given the continuous creation of enemies.
What advice do you give me better to continue with the sprite or look for a solution with the lines of the raycast?
I was wondering how do I make this cone …
do I have to repeat the event several times to create the raycast lines or is there a way to indicate it directly from the above event in question?

No, it’s a lot less processor intensive than raycasts - it’s just a collision detection, something that GDevelop is good at doing quickly. Raycasts require a lot more processing and calculation.

Just doing continuous raycasts will slow your game and make it lag if you have quite a number of raycasts

The method you should look at is a cone shaped hidden sprite to determine if the target is within range, and then only do a raycast on the target objects that collided with the cone to check it there are no obstacles in the way (i.e. make the raycast a sub-event of the collision event).

Better still is to find all enemies within a distance of the target, and then do the cone collision check on those (as a sub-event) and then do a raycast (a sub-event of the collision sub-event). This will quickly narrow down the number of enemies that need to use the raycast, and reduce the amount of processing time (and therefore any potential lag).

1 Like

Ah ok I was convinced the opposite … I’ll try to do as you advised me! thanks again

sorry it’s always me :slight_smile:
can you tell me where I’m wrong as the enemies created later at the beginning of the scene are not created the cone vision object

That won’t work because the Trigger once works for the first nemici you iterate over, and not for the subsequent ones; the trigger once condition has been met, GDevelop doesn’t care about it being a different nemici.

Add the visionfield object when you create the nemici. In that event, you have just created the nemici, and GDevelop will work with that one object. It’s the logical place to put - if you create an object and it needs other objects associated with it, then do it all in one place. Don’t spread it throughout the events sheet.

perfect so I create enemy and vision field together in the same event