How to visualize Field of View of enemy (like in Commandos 2 game)?

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

I’m using an Area2d and CollisionPolygon as FieldOfView area (object’s visible for bot only if collide it).

But it visible only of debug enabled. How to create visual element that will visualize it in game? And also I want it to draw with different colors depending from enemy AI state.

What should I add to my enemy scene tree?

2D case.

There is my game: https://www.youtube.com/watch?v=SCJXTb0WSww

I need some in-game element instead of debugging polygon.

:bust_in_silhouette: Reply From: Varden

I do not have much experience, but I can describe my way if I try to implement it.
As I can see you check if you are in CollisionPolygon and then check if enemy can see you.
I recommend you to check this video (and other from series) https://www.youtube.com/watch?v=73Dc5JTCmKI. You can try to use raycasting to draw polygon and use it for visualisation, and maybe as CollisionPolygon itself, o you do not need to check if enemy can see you.

I meant how to draw that polygon. So, I need to add something that will be drawn on UI and moves with the bot.

Robotex | 2020-08-03 13:16

But how did you create polygon from the video you shared?
You have two ways of creating nodes in godot, one way is just manually, other way is with code.
In your way you can create children of enemy manually: CollisionPolygon for detecting a target, Polygon2D for visualisation (you can fill it with colour, or use texture). Next add in script if enemy moves, then update PoolVector2Array (The polygon’s list of vertices) with generated vertices (look to the video I shared how you can generate vertices), then update polygon properties for CollisionPolygon and Polygon2D (you can check class documentation for them)
Updating vertices may not be so easy, you need to create rays, check their collisions and do other stuff.

Varden | 2020-08-03 13:42

:bust_in_silhouette: Reply From: jgodfrey

I think you’re looking for the Polygon2D node. That’ll render a specified polygon into your game world. You’d just want to give it a list of vertices that matches your existing CollisionPolygon2D node.