I have a question about enemy behavior in a topdown game. The goal is: if the player is in the viewport of the monster (so the player is close to the monster + there are no walls/obstacles between them), the monster should follow to player. I tried to do this with rayracing, and it kind of worked.
The issue is: Not all monster instances behave independently. Only one instance reacts if the player is in sight and the other instances follow that one «mother» monster.
With your code, i think the enemy could shoot a invisible bullet in one direction, if the bullet collides with the player it moves the enemy in the direction of the player and delete the bullet, if it collides with a wall it delete the bullet.
As Reborn mentioned you need several raycasts for the outline of the cone and the area inside. I read somewhere that a lot of raycasts can be potentially problematic for the performance. Using an invisible sprite for simulating the field of view is often the easier and better method.
sorry for bothering again, so my idea is create the vision object on enemy and follow it.
here works, in some way, i can create the vision object for each instance of enemy
but i get infinite copies of the object, and it doesnt works with trigger once because only create the vision object on the first enemy.
‘Trigger once’ and the loop-conditions don’t work together. I would probably just create the vision-objects at the beginning of the scene and not bother to create and delete them based on the enemies’ distance to the player.
If you want to do this, though, you could just check the distance of enemy and player first (here you should add a Trigger once-condition) and then, as a subevent, you could add the loop for creating and deleting the objects (you would need it twice: for creating the field of view an for deleting it for all enemies).