- Home /
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?
Thanks!
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.
// 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.
are you sure you're not mixing local with global position?
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?
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:
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");
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?
Is that "Offset" shown in the box collider your correction?
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... ;)
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.
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
Ok, I set the rotation to 0 (the Z-rotation to 0) and it does the same thing. Take a look:
Your answer
Follow this Question
Related Questions
Layer Mask on raycast2d not working? 1 Answer
Raycast2D will point right, but won't point left when player turns. 1 Answer
2D Raycast effect only what it hits? 2 Answers
Raycast for a 2D objects 0 Answers