- Home /
PolygonColliders act different on a build
Hi guys, Iv'e just encountered a very weird problem.
(BY CODE) I'm instantiating new game object, then adding a sprite renderer component and attaching a sprite object, and only after the sprite is fully loaded, I add a PolygonCollider2D, to fit the sprite iv'e just added.
That works fine on the editor, but when I run it on an iOS device, the colliders seem to be offset. here is a screenshot to demonstrate the results on both platforms - Unity Editor and iOS Device:
look at the cat object. in Unity editor, I can drag it to the edge of the screen with no gap. the collider perfectly matches the sprite.
in iOS device, the picture shows you me, trying to drag the cat object as close as possible to the screen edge. for some reason, the feeling is that the collider is OFFSET, so it can't be dragged to the screen's edge.
Here's the code that adds the sprite and collider:
currentPiece = new GameObject();
currentPiece.transform.position = PieceDropVector();
currentPiece.AddComponent<SpriteRenderer>();
currentPiece.AddComponent<Rigidbody2D>();
currentPiece.AddComponent<PieceBehavior>();
currentPiece.GetComponent<SpriteRenderer>().sprite = physicalPieceData.spriteOBJ;
currentPiece.GetComponent<SpriteRenderer>().sortingOrder = 3;
currentPiece.GetComponent<Rigidbody2D>().interpolation = RigidbodyInterpolation2D.Interpolate;
currentPiece.GetComponent<Rigidbody2D>().collisionDetectionMode = CollisionDetectionMode2D.Continuous;
currentPiece.AddComponent<PolygonCollider2D>();
currentPiece.GetComponent<PolygonCollider2D>().sharedMaterial = pieceMaterial;
does anyone have a clue about what went wrong? Note that I didn't change anything! right after building from unity I built the project on xCode.
Please give me an idea ho to solve this. Thanks!
are you colliding with the right edge of the screen?
Also why are doing this through code exactly? you could just instantiate a premade prefab no?
now that you found the answer... (on FB) why don't you post it and close the question? :)
Answer by FranzP3 · Jan 22, 2018 at 10:37 PM
Same here... Polygon collider added via script works great in the editor, then sucks in the built game (Android).
I actually don't know why this happens!
@shmulik.pangolin did you find a way to solve this?
Answer by NoobGaming · Jan 22, 2018 at 10:59 PM
Not by any means any kind of pro but off the top of my head it maybe a font thing? The font on the device maybe creating a edge padding around the text to guarantee it to fit within the designated space and the collider is calculating that padding and creating white space? Maybe? I don't know just spit ball here. :) Do you have "Best Fit" checked on the text in the inspector? Hopefully it helps you find an answer!