WorldToScreenPoint and enemy target issue

Hello,
I have a question about Camera.WorldToScreenPoint.

I actually want to draw a “target box” around the enemy. The canvas with the box image is a prefab, and the enemy has a script that instantiates the canvas as child of the enemy itself (not the best way but I’ll keept it this way for the moment).

Now the 1st problem is: since I use WorldToScreenPoint to translate the 3D position of the enemy into the 2D position on screen, the Z coordinate is missing and I see 2 target boxes, one around the enemy and the other one on the opposite side of my view. How can I avoid that? Should I deactivate/hide the canvas when the enemy renderer isn’t visible? Or is the a better way to do that?

2nd problem: at a certain distance, the target box disappears even if the enemy is still visible. I’m definitely below the far clipping plane… so, why is it happening?

Thank you in advance.
:slight_smile:

So, I tried to hide the target box when the enemy is not visible (using Renderer.isVisible).
I can’t get it to work using HDRP. It looks like there’s always a camera seeing the object (I knew it does that in the editor, but in my case it does that in the build as well), so the Renderer.isVisible is always “true” and the target box never disappears.
Tried it on a simple Built-in render pipeline project and it works.
Any ideas?
Thank you in advance.