- Home /
Render if Object is behind obstacle?
Simply put: The Pink-Arrow Effect from GTA2 - Picture below, under the tags.
[1]: /storage/temp/29201-1.jpg
In words: When Object becomes obstructed (is behind obstacle), render a Sprite over it (here - the Pink Arrow).
I found the "isVisible" command with all of it's options, but it only works when something is "in the range" of the Camera View - not checking if it's behind obstacle or not.
There are some Shaders which can be seen through walls and they are working fine (Silhouette Diffuse, for example). But I found no way to turn them off when the Character "is" seen (so the Arrow is "always" on the character's head, even without obstacles in way).
And worst of all - it is needed to be used on the Enemies, so they have an indicator (a similar arrow, just red) of where they are, when they stand behind cover.
Help...?
You can raycast Physics.Raycast() or Physics.LineCast() between the character and the camera. The more cast you do, the more accurate your detection will be. For 2D, raycasting from the four corners and the center of the character will be pretty accurate. For 3D, the center and the eight corners of the bounding box will be pretty accurate. Note you likely don't have to do this every frame. 5 or 10 times per second is likely enough. If you use Linecast between the character and the camera, any failure to find an object will indicate the camera can see the character.
I'll try this option, but with, for example, 30-50 enemies on screen this could be problematic. It'll be my emergency plan - more ideas are still welcome. :)
I don't know your game, but I'm guessing that much of the time either 1) the enemy will not be visible to the camera (isVisible is false so you don't have to raycast), or 2) a raycast from the center of the enemy will find the camera (i.e. the first raycast from an enemy will find indicate the enemy is visible). If my guesses are true, combined with a less frequent check than every frame, the raycast load will be acceptably low. Back of the envelope calcualtipn with these assumptions: given that you want to check all your enemies five times per second, and a frame rate of 60 fps, you end up with a Raycast load of about 5 raycasts a frame.
Answer by Vel_1828 · Jul 16, 2014 at 01:53 PM
Robertbu was right - using a simple Yield WaitForSeconds coroutine (random float # seconds between 0.4 and 0.5) still makes it CPU-friendly and still everything works well. Thanks a lot! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154038im_/https://answers.unity.com/themes/thub/images/avi.jpg)