Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by SoBiT · Feb 15, 2013 at 01:32 PM · cameraraycastobjecthitvisible

Is object at least partly visible?

Hi, I need to check if my camera is able to see a particular object. It doesn't need to see the whole object, it's enough if it's only partly visible. At the moment I'm using the IsVisibleFrom() Function. But this function only checks if the object is rendered. Sadly it's also rendered if another object obscures it completly. I think I will have to use Raycasts but my camera is not only a point, and that's why I can't find a solution.

Comment
Add comment · Show 7
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 Kirlim · Feb 15, 2013 at 02:48 PM 0
Share

Visibility is always complex stuff =D. Raycasts should work, but what do you mean that your camera is not a point? Its orthographic?

As an alternative, and somewhat costly way, you can try rendering a simplified world version in a secondary camera, invisible to the player. For example, walls are red and the object is white. If there is a single white pixel in the image generated by the secondary camera, the object is visible. Shaders experts might have a way to make this reasonably fast, I think.

avatar image SoBiT · Feb 15, 2013 at 02:51 PM 0
Share

Well.. the camera is a point but its view is not.

The way with the second camera sounds difficult. I prefer using Raycasts. Do you have any idea how to check this?-

avatar image Kirlim · Feb 15, 2013 at 02:58 PM 0
Share

As long the obstacles have colliders and/or rigid bodies, you can try raycasting from the camera position to a set of edge positions of the object you want to check its visibility. If one or more of the raycasts returns the object itself (or nothing, if the object does not have colliders) and no obstacles, then the object is visible. If your camera is not orthographic (or anything close to it), this approach should work. You might need to check distances because of the far and close planes of the camera.

avatar image SoBiT · Feb 15, 2013 at 03:06 PM 0
Share

So would you recommend to add a cuboid arround the object and check all 8 edges?

avatar image Kirlim · Feb 15, 2013 at 03:30 PM 0
Share

The simplest possible shape to represent your object. If you have to check against many objects at once, you might try to simplify to a rotated box and use, for example, 4 vertices of the box.

The problem with raycasting is a situation where, for example, all of the edges used to check visibility are behind a wall, but other parts of the object are actually visible. So choose the number of edges and structure of your scene in order to $$anonymous$$imize or avoid this problem.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Feb 15, 2013 at 04:12 PM

You can cast a line from the eight corners of the renderer.bounds to the camera. This is not a perfect solutions since it is testing the bounding box rather than the mesh itself. I'm not sure how expensive it is, but here is a function:

 bool IsBlocked()
 {
     Vector3 v3Corner = Vector3.zero;
     Vector3 v3Center = renderer.bounds.center;
     Vector3 v3Extents = renderer.bounds.extents;
     
     v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;
     v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;    
     v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;    
     v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;    
     v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;
     v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;    
     v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;    
     v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
     if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
         return false;    
     
     return true;
 }
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Check if specific camera sees object 0 Answers

How to shoot a raycast with third person 1 Answer

Camera Zoom in&out 2 Answers

Activate object if visible and Deactivate object if invisible 2 Answers

Destroying object using his name and raycast 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges