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
2
Question by Kvazir · Jan 09, 2012 at 11:14 PM · sphereoverlap

Physics.OverlapSphere

Does anybody know, how this works. I need an array with all colliders touching or inside the sphere. Everything I found just this http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html please.help!

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
4

Answer by aldonaletto · Jan 09, 2012 at 11:31 PM

That's exactly what you want, and the example is found in AddExplosionForce (ok, it should be in OverlapSphere as well...).
OverlapSphere(position, radius[, layerMask]) creates an imaginary sphere centered at position and with radius radius, and returns an array with all the colliders touching or inside the sphere. Only the colliders in the layers enabled by layerMask are returned (enables all layers if layerMask omitted). The example code is the following (should be attached to a explosion prefab):

var radius = 5.0;
var power = 10.0;
function Start () {
    // Applies an explosion force to all nearby rigidbodies
    var explosionPos : Vector3 = transform.position;
    // get all colliders touching or inside the imaginary sphere:
    var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
    for (var hit : Collider in colliders) {
        if (!hit) continue; // avoid null references (should not occur, but...)
        if (hit.rigidbody){
            hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
        }
    }
}
Comment
Add comment · Show 1 · 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 code-blep · Dec 06, 2012 at 07:55 PM 0
Share

Brilliant example. Thanks Aldo.

avatar image
1

Answer by koirat · Jul 29, 2012 at 04:39 PM

In unity documentation Script reference about Physics.OverlapSphere there is this NOTE:

NOTE: Currently this only checks against the bounding volumes of the colliders not against the actual colliders.

This function is a joke. It's name is misleading.

So I'm asking the question is there a way to tell if my static colliders are inside a sphere. Primitive colliders would be enought, let not be to demanding.

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 Bunny83 · Jul 29, 2012 at 04:48 PM 0
Share

Well, this isn't really an answer to the question. Do you actually need to know which colliders are completely inside or if they overlap? Completely inside isn't possible at all. This would be very complex.

Unfortunately you can't test collisions manually, so OverlapSphere is the best built-in solution. It you want to improve the accuracy, you have to do some manual collision checks. The easiest one would be a sphere collider, since you can simply check the distance againse the two radii. All other collder shapes are not that easy. For a box collider you might need to test each surface

avatar image koirat · Jul 29, 2012 at 06:01 PM 0
Share

Well it was not exactly the answer. It was more like a bump to the question that was answered giving a bad impresion that this function is actually doing what it's supposed to.

What I want to do is exactly like the explosion example, I don't ned to know if collider is completely inside the sphere, just to overlap is ok. As I said before it does not have to be a mesh collider, I know how difficult it is to check for a collision in concave colliders.

But seriously not checking for a Prmitive Colliders like spheres , cylinders, boxes, capsules in my opinion is a big flaw.

Unity is not a "low level" engine it should have built in all combinations of overlap/collision checks for primitive colliders.

And just checking for a bounding box (I guess it's Axis aligned) will give a very bad precision.

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

8 People are following this question.

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

Related Questions

overlap half sphere? 0 Answers

How is OverlapSphere used? 2 Answers

Closest target acquisition using Physics Overlap Sphere script not working 1 Answer

OverlapSphere is really confusing 0 Answers

Sphere Overlap EachOther 0 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