• 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 slammin · Mar 05, 2017 at 03:37 PM · 2draycastraycasthit2dheight

BoxCollider2D is mysteriously offset by a slight but obvious amount

I am using a Raycast2D to interface with my BoxCollider2D. The editor says that BoxCollider2D is in a certain position, but when I test out the program the Raycast suggests that the BoxCollider2D is offset from that position by a slight but obvious amount. Why is my BoxCollider2D being offset?alt text

Thanks!

boxcollideroffset.png (295.0 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 turndapage · Mar 05, 2017 at 04:23 PM 0
Share

Could you post some of your code on how you are getting the box collider using raycast? Is your object a child of another object? You may need to offset the position you got by your objects transform.

avatar image slammin · Mar 05, 2017 at 05:59 PM 0
Share
       // Update is called once per frame
     void Update () {
 
         // Perform a raycast from the UICamera to discover what the
         // mouse is hovering over
         Ray ray = cameraUI.ScreenPointToRay(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
 
         if(hit != null && hit.collider != null)
         {
             // this converts cube to sphere
             if(hit.collider.name == "SpeedControlLever")
             {
                 Debug.Log("hit lever");
             }    
             else
             {
                 Debug.Log("didn't hit lever");
             }
         }
     }

Offsetting the BoxCollider2D does fix the symptoms of the problem, but, does not address the mysterious underlying cause.

avatar image hexagonius slammin · Mar 05, 2017 at 06:20 PM 0
Share

are you sure you're not mixing local with global position?

avatar image Glurth · Mar 06, 2017 at 06:02 PM 0
Share

Allow me to suggest a few "sanity-checks": Use Debug.Log to display the mouse position, AND the bounds of the collider when you hit (https://docs.unity3d.com/ScriptReference/Collider2D-bounds.html).

You can also use Gizmos.DrawWireCube to display the bounds in the scene view, to confirm it matches up with what the editor shows.
Better yet, create a Cube primitive, assign it's position to the bounds center, and scale to the bounds size. This object will be visible in the camera view, and should show the area of the collider.

@Hexagonius how would local/global stuff affect that code?

2 Replies

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

Answer by slammin · Mar 10, 2017 at 07:22 AM

Ok, so, I found a solution. It appears the using raycasts do not collide correctly with BoxColliders2D that are part of a Canvas system. So, the solution is simply to set up a button and use the event system that Unity has created for this task. Take a look:

https://vid.me/u3nz

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
avatar image
0

Answer by turndapage · Mar 05, 2017 at 09:28 PM

How I do it in a 2D Game would be the ScreenToWorldPoint camera function. You can then check if the point is within the bounds of your collider.

Example:

 Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         if (col.OverlapPoint(mousePos))
             Debug.Log("The mouse is inside the bounds of the box collider");
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 slammin · Mar 06, 2017 at 05:44 PM 0
Share

Hi turndapage, Thanks for your response. I just implemented your solution and the exact same thing happens: there is a subtle yet obvious offset (it is the same offset that occurs when using ray casts.)

I'm not sure how to deter$$anonymous$$e if the calculations are using local or world space like hexagonious suggested...

I am bypassing the "Event System" which Unity is encouraging that we use, perhaps that is why this error is occurring. $$anonymous$$aybe attempting this through the event system will work...

Any other thoughts as to why the BoxCollider2D is offset by a subtle margin?

alt text alt text

screen-shot-2017-03-06-at-103706-am.png (118.3 kB)
avatar image Glurth slammin · Mar 06, 2017 at 06:57 PM 1
Share

Is that "Offset" shown in the box collider your correction?

avatar image tanoshimi Glurth · Mar 06, 2017 at 08:04 PM 0
Share

Exactly what I was just wondering. If this "slight offset" is equal to, say, (81.15407, 95.41795), I think we may have found the solution... ;)

Show more comments
avatar image tanoshimi · Mar 07, 2017 at 07:11 AM 0
Share

So in your screenshot it suggests that the collider is offset only in the x dimension. In your most recent comment however you are adjusting the y component of your collider offset by 30 while leaving the x alone. Is your object rotated? 2d colliders don't play well with rotated objects.

avatar image slammin · Mar 07, 2017 at 04:47 PM 0
Share

Oops @ tanoshimi -- I made a mistake. It appears that the object is being offset on the Y-Axis, not the X-axis as I said earlier.

Is it rotated? Yes. I will see what happens if I use the 0 rotation. $$anonymous$$aybe the rotation is messing up the BoxCollider2D. Good idea.

Also, here is a video showing exactly what I'm saying: https://vid.me/eN2w

avatar image slammin · Mar 07, 2017 at 04:53 PM 0
Share

Ok, I set the rotation to 0 (the Z-rotation to 0) and it does the same thing. Take a look:

https://vid.me/Tdby

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Layer Mask on raycast2d not working? 1 Answer

Raycast2D will point right, but won't point left when player turns. 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

2D Raycast effect only what it hits? 2 Answers

Raycast for a 2D objects 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