I’m checking to see if a point is within a run-time generated PolygonCollider2D
on a run-time generated Sprite
and am using OverlapPoint(point)
to check this. I get the exact expected behavior on the Unity player, but when compiled into an APK and loaded on my Android phone, OverlapPoint(point)
returns true in what looks to be like a BoxCollider2D
around the Sprite
. The Sprite
is a leaf and not a box.
Behavior on Unity Player:
Behavior on Android device:
Anyone have any idea what’s going on? I’m lost!
Thanks
p.s. yes it’s the most updated freshly-compiled apk
OKAY I FIGURED IT OUT GUYS. GOSH DANG THAT WAS ROUGH.
After hours and hours of scouring the web and piecing everything together I was able to figure out the problem: PolygonCollider2D is more inaccurate when created and scaled at runtime on a device
I was creating a new Sprite each time, and adding a PolygonCollider2D to it, which worked fine in the editor. However it does not work fine on my mobile device, generating a generally much larger collider.
###I call this fix The Angry PolygonCollider Bandaid:
- Force the PolygonCollider2D to be generated by the Unity Editor
   Drag your image onto the object hierarchy to create a new Sprite. Scale the Sprite at (0,0,0) from within the inspector. Click ‘Add Component’ and add a PolygonCollider2D.
- Clone the created Sprite that has the Unity Editor generated PolygonCollider2D
   Create a public reference to your Sprite from within your script, along the lines of public GameObject prerenderedSprite;
. Drag your Sprite from the inspector onto the prerenderedSprite
variable of your script.
   Clone the object by GameObject clonedSprite = Instantiate(prerenderedSprite);
- Scale the new sprite to your liking
   Now that you have your sprite, you can scale it dynamically without fear of an inaccurate PolygonCollider2D.
    clonedSprite.transform.localScale = new Vector2(scaleX, scaleY);
Your sprite will be just as accurate as it is in the Unity Edtior.