Check if object is visible on the screen and not behind another object
Well I have a problem with making a horror game. In that game, when you look at the enemy and he is, of course, not behind a wall or similar, it detects that it is being watched. Now I do not have problem with detecting the visibility of an object with isVisible or WorldToWiewportView, but I have a problem if an enemy is behind some other object. And I do not know how to use Raycast in that situation, because I do not want to detect if the object is directly in front of the player, rather than detecting if even a portion of an object is visible and not behind any other object. I hope I explained everything. Does anyone have any ideas?
Answer by ThatHomelessGuy · Nov 08, 2018 at 04:50 PM
I'm not sure how efficient it would be but creating a rectangular array of evenly spaced points that cover the bounds of the enemy to raycast at one after the other would probably do the trick. Just check that you are hitting the model or sprite first and if any one of them returns true break to next frame (to save on cpu casting when it doesn't need to) and do as you normally do when the enemy is "seen".
It also depends on how many enemies you have in potential view. You could also step over any raycasts that might fall out of bounds of the screen.
Not sure on the exact code you'd use for this but that's the fun of creating games, you can work out implementation once you have a plan in place.
Edit : You could also only process a certain number (maybe 1/4) of casts in the array for each enemy each frame and just keep the place in the array stored until the next frame if it's slowing things dowen too much.
Well there is a problem with rectangular arrays, and that is of course performance. I actually found something that might help and that is spherecast. Thank you for helping anyways!
There should be no issues with performance using rectangular arrays themselves. I literally use them and cube arrays to store an entire maps data. Quite literally millions of array indexes at a time.
There could be something in how you implement the arrays unless of course you are talking about the performance issues with raycasting to them. For raycasting I'd keep the array to 8*8 or 16*16.
But if spherecast works for you then great though I'm not 100% sure how it would benefit in checking obstructed view I might have different standards in $$anonymous$$d for what qualifies as obstructed view.