• 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 Projectgoo · Mar 28, 2014 at 10:32 PM · colliderhitoverlapsphere

OverlapSphere picking colliders that are not inside

I have one issue that I dont understand?!

I made a simple test: I have in my scene box and camera. The box has a collider component and it also has script that implements interface ISmashable. My idea was to move the camera and to hit the box, and when I hit it, directly it sends the camera to a start position of:

 new Vector3(transform.position.x, 15, transform.position.z);

for that purpose I created this script which is directly called in the update method of the camera:

 void Hit()
     {
         var coll = Physics.OverlapSphere(transform.position, 0.5f);

         foreach (var item in coll)
         {
             var smash = item.GetComponents(typeof(ISmashable));
             
             if (smash.Length == 0)
             {
                 return;
             }
             
             for (int i = 0; i < smash.Length; i++) {
                 Debug.Log("hit");
                 transform.position = 
                     new Vector3(transform.position.x,15,transform.position.z);
             }
         }    
     }

The problem is that when I hit the box it does not stopping writing "hit" in the console even when I am actualy far away from the box, and basically Physics.OverlapSphere should return me empty array.

Please can anyone explain me where I'm do wrong ?!

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 robertbu · Mar 29, 2014 at 04:28 AM 0
Share

I see nothing wrong with the code. I suggest you put a sphere around this game object without a collider with a size of 1.0 (radius of 0.5) to visualize the overlap. In addition, output the name of the game object for each collider

avatar image Projectgoo · Mar 29, 2014 at 08:25 AM 0
Share

Thanks for the comment, well I put the sphere around the camera without any collider, yes. And the box has collider, but it still doesnt fix the problem, it keep running and doesn't stop.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

ERROR: why is showing red OnControllerColliderHit? 2 Answers

Does OverlapSphere() return the colliders in any particular order? 1 Answer

Hit collider problem 1 Answer

CapsuleCollider : How to get the info which part is hit? 2 Answers

Destroy objects within ordered Array by line of sight 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges