• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ThePelicanBird · Nov 08, 2018 at 03:26 PM · raycastrenderinggamerendererhorror

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePelicanBird · Nov 08, 2018 at 05:56 PM 0
Share

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!

avatar image ThatHomelessGuy ThePelicanBird · Nov 08, 2018 at 06:30 PM 0
Share

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 mind for what qualifies as obstructed view.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

238 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Horror game 1 Answer

Change color of sprite when hit by raycast [SOLVED] 2 Answers

Universal renderer not working at all 0 Answers

Enemy AI following player help 0 Answers

Need a Raycast2d to point where my ship is looking 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges