+1 vote

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.

in Engine by (246 points)
edited by

2 Answers

+3 votes
Best answer

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.

https://docs.godotengine.org/en/stable/classes/class_polygon2d.html?highlight=polygon2d

by (10,868 points)
selected by
+1 vote

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.

by (59 points)

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.