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 DarthChimay · Feb 26, 2014 at 12:29 AM · c#collider2dphysics2d.raycast

Having issue with Physics2D.OverlapCircleAll returning incorrect collider

I'm creating a tower defense game with the new 2D stuff and I've run into a really strange issue that I'm sure is with my script.

I have multiple tower placement points, represented by BoxCollider2D objects on the placementLayerMask. When I place a tower on one of these points (e.g. PlacementPad_1), then click on a nearby placement pad (e.g. PlacementPad_2), the script sometimes believes I am still tapping on PlacementPad_1. This occurs when the CircleCollider2D (which is on the default layer) attached to the tower for targeting overlaps the BoxCollider2D. Sometimes it corrects itself if an enemy leaves the placed tower's targeting trigger, but I can't tell if there's an actual correlation there.

Here is my script with some comments. I'm certain the problem lies somewhere in here, since I'm still relatively new to C# scripting, but I can't seem to tease it out. I've commented out countless portions of the script and tried several different things, but I always get the same error.

 void Update(){
     if(Input.GetMouseButtonDown(0)){
         //When the mouse button is clicked, we cast a 2D ray to see where we've hit
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

         //Debug.Log(hit.collider.name);

         //If the tower purchase menu is not already open, then...
         if(!purchaseMenuOpen){
             //we check to see if we've actually hit something. If not, we return. I found this necessary as I was 
             //getting spammed with a null exception when I didn't click on a placement pad. I tried commenting 
             //this out to see if it was the issue. It was not.
             if(!hit){
                 return;
             }

             //Creating an array of all colliders found in a circle with a diameter of 0.02f that is created under the
             //mouse click. Any collider not in the placement layer is presumably ignored. I have tried different sized
             //circles, going smaller and smaller, but it  hasn't fixed the issue.
             Collider2D[] col = Physics2D.OverlapCircleAll(hit.transform.position, 0.01f, placementLayerMask);

             //Debug text to see what has been placed in the array. This is where I first notice the error. It seems as
             //though, if there's a CircleCollider2D on top of the placement pad's BoxCollider2D, the array is getting
             //interfered with as in the description above.
             for(int i = 0; i < col.Length; i++){
                 Debug.Log("col " + i + " is " + col[i].name + " and size is " + col.Length);
             }

             //Had to make a bit of an assumption here. With the size of the circle, the first object in the array should
             //be the object that was clicked on, assuming something was clicked on. This might be overly redundant, but
             //commenting it out does not fix the issue. From here on out, the script works fine so long as it has the correct
             //object as the first in the array.
             if(col[0] != null){
                 Debug.Log(col[0].gameObject.name);
                 lastHitObject = col[0].gameObject;
                 //Debug.Log(lastHitObject.name);
                 focusedPlacement = lastHitObject.GetComponent<PlacementScript>();
                 Vector3 padPos = Camera.main.WorldToScreenPoint(col[0].transform.position);
                 //Debug.Log(padPos);
                 padPos = uiCam.ScreenToWorldPoint(padPos);
                 //Debug.Log(padPos);
                 padPos.z = 0f;
                 //Debug.Log(padPos);
                 if(focusedPlacement.isOpen){
                     purchaseMenu.transform.position = padPos;
                     purchaseMenu.gameObject.SetActive(true);
                     purchaseMenuOpen = true;
                 }
             }
         }
 }

Thanks for taking a look at this!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DarthChimay · Mar 11, 2014 at 05:48 AM

I figured it out, finally, and I'll post what I did here in case others run into the same problem. It's fortunately pretty simple.

If the colliders being raycasted have the same z coordinates, then OverlapCircleAll doesn't return them. I don't know why it would return the colliders I previously interacted with, but there you are. I'm also unsure if this is as intended or not.

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

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

Where i can find detailed description of Unity C# function? 2 Answers

Distribute terrain in zones 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

c# how to refresh my targeting list 2 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