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 vagos21 · Sep 16, 2013 at 03:25 PM · returnspherecolliderstrangephysics.overlapsphere

Physics.OverlapSphere returns a strange "centerSphere" collider?

Hello, i want to trace some possible "snaps" in my code, and after some tests i decided to use OverlapSphere instead of OnTriggerEnter, which was killing performance. what is also good, OverlapSphere doesn't need to have any rigid bodies attached to any of the script's gameObjects.

so, as seen in this image, i'm using small sphere colliders that are in the available slots, and then each one has a script that does a Physics.OverlapSphere every one second or so, to avoid running it every frame.

and even though the code runs fine, sometimes it crashes and when i print the name of the "traced collider" i get the name of "centerSphere". And after searching, and knowing that i used such a name nowhere in my code, didn't even find anything on google about such a thing, i came here

anyone have the slightest idea what that might be?

here's also some most critical part of the code that prints this:

 function detectAutoConnections(){
    var hitColliders = Physics.OverlapSphere(transform.position, coll.radius, 11);
    for (var i = 0; i < hitColliders.Length; i++){
       Debug.Log(gameObject.name + " - " + hitColliders[i].gameObject.name);
    }
 }

i invoke this function once i enable these small spheres

 InvokeRepeating("detectAutoConnections", 0.0, 1.0); //start detecting auto-connections immediately, every 1 seconds

alt text

i get "autoConnector - centerSphere" autoConnector is correct and good, centerSphere-i have no idea... i'm filtering collisions on layer 11, and only these little sphere colliders belong to it. so i don't see why it finds this strange centerSphere object..?

autosnaps.png (77.9 kB)
Comment
Add comment · Show 4
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 robertbu · Sep 16, 2013 at 10:53 PM 0
Share

A couple of thoughts. Layers are masks, not integers. An easy way to handle the mask is to declare a variable of type Layer$$anonymous$$ask and then set it in the inspector:

 var mask : Layer$$anonymous$$ask = 0;

You you could write a bit of code that finds the game object called 'centerSphere' and makes something happen...moves it to the center of the screen, flashes it, something...or simply search for the name in the hierarchy at runtime.

Also note that OverlapSphere() does not check the spheres. It, "Currently this only checks against the bounding volumes of the colliders not against the actual colliders."

avatar image vagos21 · Sep 17, 2013 at 08:53 AM 0
Share

Thank you mate! Your idea for figuring out thhe centerSphere object was good :) it was an object i created for my movement/rotation gizmo, and working with unity 10 hours a day can make your $$anonymous$$d go crazy :D

about the layer masks, i'm using the layer manager to set my layers and assign gameObjects to them. like the image below:

alt text

and then in the code i do something like

 myGameObject.layer = 11;

is this the wrong way of using the layers in the overlapsphere function?

layerslist.png (18.1 kB)
avatar image meat5000 ♦ · Sep 17, 2013 at 11:16 AM 0
Share

You should convert to answer robertbu

avatar image vagos21 · Sep 17, 2013 at 11:17 AM 0
Share

agree, it should be converted to answer and i'll tick it right to at least get it out of the unanswered list :)

1 Reply

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

Answer by robertbu · Sep 17, 2013 at 09:25 AM

The problem is in this line here:

 Physics.OverlapSphere(transform.position, coll.radius, 11);


The '11' should be '1 << 11'.

As mentioned above, it is easier to specify:

 var mask : LayerMask = 0;
 Physics.OverlapSphere(transform.position, coll.radius, mask);

Then you set 'mask' to layer 11 in the inspector.

Note, given what you are doing, it might be easier to just maintain a set of points and use distance to do your calculation. You could use a set of empty game objects with tags to find the positions.

Comment
Add comment · Show 3 · 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 vagos21 · Sep 17, 2013 at 10:22 AM 0
Share

Thank you for this very useful information about layer masks, it's something i wasn't aware of :)

so you say there's a faster and even easier way than using overlapsphere to trace distances between these points? the problem is, there would be about 2000 of them (moving, so i can't really maintain sets of points?), and i can't just go around and calculate distances between them all, wouldn't that kill performance? something in my guts tells me overlapsphere is the most efficient way of doing it... but i'd happily try other ideas that could make it more effectient too!

avatar image robertbu · Sep 17, 2013 at 11:31 AM 0
Share

If OverlapSphere() checks against the bounding box as advertised, then checking distance (or squared distance) is likely to be more efficient. Ins$$anonymous$$d of the colliders you have now, place an empty game object at the position the colliders now hold. You can use GameObject.FindGameObjectsWithTag() and then compare the distance between the points. Note that with OverlapSphere() your code may be simpler, but Unity is still having to do the processing under the hood.

This assumes you need more performance. What you have now may be good enough.

avatar image vagos21 · Sep 17, 2013 at 11:42 AM 0
Share

i fear the performance will never be enough, there will be a day when someone tries to make a complex build (it's a lego style toy) that would go to up to 10000 traces of distances between the connection points... so you say i remove my colliders and forget the whole overlapSphere thing, and try to manually check the distances between all of them :)

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

16 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

Related Questions

Sphere Collision Overlap Detection 2 Answers

Prevent overlapping of spawned object over AI and Player 1 Answer

Unity Editor deselects item after clicking on its controls. 4 Answers

Teleporting Player doesn't work as expected? 1 Answer

Strange things appear on Android Build. 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