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 PeterB · Apr 04, 2011 at 05:47 PM · colliderstriggersoverlapnested

How do I check that a trigger collider is *completely* nested within another trigger collider?

I have a hierarchy of trigger colliders. Some colliders are entirely separate, others overlap to some degree, and some are completely contained within another collider, sometimes three or four levels deep. The colliders are either Sphere or Box colliders. The whole thing represents location zones of various sizes: islands, regions, towns, etc.

In order to determine where a given point lies, such as the position of the player, I need to find the innermost nested collider for that point. Physics.OverlapSphere gives me a list of colliders, but when examining them, I need to treat trigger colliders completely enclosed by another collider specially.

How can I determine whether one collider is completely nested within another?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bampf · Apr 04, 2011 at 07:10 PM

Not quite sure I understand exactly what you are trying to accomplish, but...

The short answer is, since you are only using sphere and box colliders, given a pair of colliders A and B, you can test 6 points: the center of each face of the bounding box of A. If those points are all inside collider B, then A is inside B. (You can use the bounds' extents and the center of the bounding box to calculate the coordinates of those 6 points.) Oddly, there is no built-in Unity method to calculate if a point is inside a sphere collider- with a box collider you could use collider.bounds.Contains. But the sphere test is easy to write.

Assuming the geography doesn't change, you could build the zone heirarchy once as the game starts. It wouldn't be that hard to write something that walks all the colliders and tests them for enclosure. Each town could keep a link to its enclosing region(s), for example.

You might also consider manually assigning the colliders to layers. Each layer would represent a different zone type. This has a couple benefits. First, if you want to build the hierarchy you'd only have to test colliders against certain types. For example, you wouldn't need to test if an island was enclosed by a town; but you WOULD test the reverse case.

Secondly, depending on what you are trying to accomplish, you might be able to use the layers to avoid the need to store a hierarchy at all. For example, when calling OverlapSphere you could say that you are only interested in the Town layer. This would give you just the town colliders. Then repeat for other zone types as needed.

Comment
Add comment · Show 7 · 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 PeterB · Apr 05, 2011 at 07:43 AM 0
Share

I'm well aware of the fact that it's always possible to test for enclosure using discrete geometrical tests, of course - but I was looking for a simple primitive, as the idea of such a test seems like something that would be built into the engine. I'm a bit surprised to find that Unity doesn't seem to be able to test for something as simple as enclosure of geometry.

avatar image PeterB · Apr 05, 2011 at 07:47 AM 0
Share

Regarding the zone hierarchy: the tool I'm building is completely general and has an editor representation. The depth is arbitrary, and the topology might very well change, as some of the objects may be moving. The architecture I've designed handles all this efficiently and without hacks like OnTriggerStay et al - the only thing that needs to be implemented is that test for enclosure of one collider by another.

avatar image Jesse Anders · Apr 05, 2011 at 12:34 PM 0
Share

It shouldn't really come as a surprise that the Unity API doesn't include support for containment queries. There are many queries that might be of interest to developers (containment, closest points, etc.), but such queries are arguably outside the scope of what Unity offers and is intended to offer. In any case, it does sound like a custom solution might be most appropriate here; based on what you've posted at least, I'd probably just write code for whatever containment tests are needed and go from there.

avatar image Bampf · Apr 05, 2011 at 12:50 PM 0
Share

Jesse, I agreed with you in principle, and thankfully the sphere and box tests are easy to write. But I sympathize with PeterB's frustration: it's not always obvious why Unity has one thing and not another. Even your examples of out-of-scope queries Unity implements in specific cases (Bounds.Contains; Rigidbody.ClosestPointOnBounds.) It just doesn't have them for all collider types. It's not an unreasonable idea.

avatar image Jesse Anders · Apr 05, 2011 at 12:59 PM 0
Share

Yeah, I know what you mean - Unity does include the occasional 'random' functionality that kind of makes it seem like it should include other related functionality as well. But, I always view these more as 'extras' than as an indication of what should be expected necessarily :)

Show more comments
avatar image
0

Answer by marty 1 · May 05, 2011 at 03:19 PM

I think that if you have rigidbodies attached to each collider, you could also use ClosestPointOnBounds:

I.e.

var closestPointOnObjectA : Vector3 = rigidbody.ClosestPointOnBounds(objectBPosition); var closestPointOnObjectB : Vector3 = rigidbody.ClosestPointOnBounds(objectAPosition);

Then just check the distance between these two points to see if they overlap.

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

No one has followed this question yet.

Related Questions

How to Manually Test Trigger Overlap 0 Answers

Physics.OverlapBox colliding with disabled MeshCollider 1 Answer

How to have multiple colliders intersect without colliding with each other? 1 Answer

[Unity 4.6.1] Weird bug between 2 Colliders 1 Answer

changing gravity OnTriggerEnter 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