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 Tudor · Aug 12, 2014 at 10:52 PM · raycastcolliderisvisible

Cast a ray only against "isvisible" objects

The unity documentation for raycasting says that a raycast "Casts a ray against all colliders in the scene."

I only need to raycast against the colliders of objects which have renderer.isVisible == true. I'm thinking it would relieve a lot of strain on the CPU if you had a scene with a ton of colliders.

Does anyone with more unity know-how have any idea if this is possible?

Would I gain anything performance-wise if I had a script on each object (and say I have 1600 objects) which turned its collider on or off based on their own OnBecameVisible() or OnWillRenderObject() events?

Or would the only option be, implementing your own raycast/collision system which keeps track of isVisible objects on its own?

Comment
Add comment · Show 2
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 rutter · Aug 12, 2014 at 10:59 PM 2
Share

The physics engine is already pretty well optimized; it uses spatial partitioning to reduce the number of objects it needs to check at any given time. The documentation suggests a raycast is checked against "all" colliders, which is true in terms of results but not how it's calculated.

With that said, spatial partitioning is less effective when you have a very large number of colliders close together. Unless you're doing that, you probably don't need to worry about it.

If you're having a performance problem, you might consider reducing the number or distance of your raycasts, so that it has fewer colliders to check against.

avatar image Tudor · Aug 13, 2014 at 12:01 AM 0
Share

Thanks rutter, that's kinda what I wanted to hear. So let's say I have a large cube of cubes. (each with a collider) And I am standing in its center. And I cast a bunch of rays in the direction the camera is viewing. Would uniy's spatial partitioning only fetch objects spatially located in front of the camera (because that's the direction of the ray)?

And one last concern: is the spatial partitioning based on the GO.transform.position? Because my "cubes" are procedurally generated, and even though the cube mesh, mesh bounds, and cube collider are in a certain (correct) point in space, the gameobject.transform.pos is always at 0.0.0...

Raycast CPU consumption seems to be constant enough as far as I can tell. So it probably means it's working efficiently :P

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WhoRainZone1 · Aug 13, 2014 at 01:11 AM

I'd suggest you using tags on your GameObjects, as you can create Layermasks to define which tag to ignore and which not. Then you just set the tag when making it visible/invisible.

Hope that does the trick, if not there are unfortunately no other way's to tell a raycast to not even bother checking that GameObject.

Cheers, WRZ

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

23 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

Related Questions

A node in a childnode? 1 Answer

Smooth Movement on Geometry Collission? 0 Answers

Unity 4.5.2 bug changing the properties of the 2d colliders in animation 1 Answer

Don't allow raycasts to go through colliders? 3 Answers

How to determine which part of an NPC is hit? 1 Answer


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