0
\$\begingroup\$

I have a Polygon2DCollider set in my scene that I am using Physics2D.OverlapPoint(position, 0) to see if the set position collides with anything. Initially, the collider is stored in memory and then deactivated for future use. When I want to use it I do the following:

targetColliderObj.SetActive(true);

Collider2D collider = Physics2D.OverlapPoint(testPosition, 0);
if (collider == null || collider.tag != targetColliderObj.tag)
{
    Debug.Log("Unable to move down a floor.");
}

In the code above I activate the target gameobject for the Polygon2DCollider and check to see if the testPosition is within the collider. The collider is always null even though I am placing the testPosition inside of the collider. Also, I am checking on layer mask 0. The gameobject of the Polygon2DCollider is also on the Default layer (0).

\$\endgroup\$
6
  • \$\begingroup\$ Have you considered using Collider.OverlapPoint instead, to test only this single object? What do you mean when you say the collider is stored in memory and then deactivated? Depending on the deactivation you're doing, that could mean it's removed from the physics world and no longer visible to Physics2D.OverlapPoint \$\endgroup\$
    – DMGregory
    Jan 29 at 0:17
  • \$\begingroup\$ I'll have to look into trying that. Also, what I mean is they exist in the scene to begin with and are set active. When I setup things in my scene I find the collider, store it in an array, and disable it with SetActive(false). But I enable it again before I check for a collision. \$\endgroup\$
    – sabo
    Jan 29 at 0:29
  • \$\begingroup\$ Do you call SyncTransforms so the physics engine is alerted to the newly re-activated collider? Normally changes to the physics world are synched only at specific times in the frame, so if you want it to happen in the middle of your script running you need to ask for it. It sounds though like you just want a collider on a layer that doesn't interact with others, to save the fuss of activating and deactivating it each time you want to check for containment. \$\endgroup\$
    – DMGregory
    Jan 29 at 0:32
  • \$\begingroup\$ I tried setting Auto sync transforms in the project setting and that didn't work. Maybe doing it manually in code is necessary. \$\endgroup\$
    – sabo
    Jan 29 at 0:37
  • \$\begingroup\$ I just tried using Collider.OverlapPoint() and it worked. Thanks @DMGregory. If you add an answer I'll accept it. \$\endgroup\$
    – sabo
    Jan 29 at 1:10
0
\$\begingroup\$

I was able to resolve this issue by using Collider.OverlapPoint(); rather than what I was originally trying. Here is the simple change that was necessary:

targetColliderObj.SetActive(true);

PolygonCollider2D collider = targetColliderObj.GetComponent<PolygonCollider2D>();
if (collider.OverlapPoint(testPosition))
{
    Debug.Log("The collider was hit!");
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.