- Home /
polygoncollider2d does not work on device?
Hello, i'm using a polygoncollider2d on my sprites, so my collisions will be more accurate (Adhering to the outline of the sprite). this works well when i test in the editor (my sprite only bounces when the opaque pixels collide with an object), but when i build to my device, it appears to go back to be using a box collider or something else (it bounces when the outline of my square sprite tile touches an object, like its using a box collider).
anyone else ever experience this? here is the code i'm using for the class with the collisions..
public Character()
{
Physics2D.gravity = Vector2.zero;
//Physics2D.velocityThreshold = 0.001f;
physicsMaterial = new PhysicsMaterial2D();
//physicsMaterial.bounciness = 1;
bodyWeaponSprites = Resources.LoadAll<Sprite>("bodyweapongrid");
weaponBodySprites = Resources.LoadAll<Sprite>("weaponbodygrid");
bodySprites = Resources.LoadAll<Sprite>("bodygrid");
weaponSprites = Resources.LoadAll<Sprite>("weapongrid");
bodyWeaponObject = new GameObject();
weaponBodyObject = new GameObject();
bodySpriteObject = new GameObject();
weaponSpriteObject = new GameObject();
bodyWeaponObject.AddComponent<SpriteRenderer>();
bodyWeaponObject.GetComponent<SpriteRenderer>().sprite = bodyWeaponSprites[0];
// bodyWeaponObject.AddComponent<Rigidbody2D>();
bodyWeaponObject.AddComponent<PolygonCollider2D>();
bodyWeaponObject.GetComponent<PolygonCollider2D>().sharedMaterial = physicsMaterial;
bodySpriteObject.AddComponent<SpriteRenderer>();
bodySpriteObject.GetComponent<SpriteRenderer>().sprite = bodySprites[0];
bodySpriteObject.AddComponent<Rigidbody2D>();
bodySpriteObject.AddComponent<PolygonCollider2D>();
bodySpriteObject.GetComponent<PolygonCollider2D>().sharedMaterial = physicsMaterial;
bodySpriteObject.AddComponent<Hit>();
bodyWeaponObject.transform.parent = bodySpriteObject.transform;
//bodySpriteObject.transform.parent = bodyWeaponObject.transform;
Physics2D.IgnoreCollision(bodySpriteObject.GetComponent<PolygonCollider2D>(), bodyWeaponObject.GetComponent<PolygonCollider2D>(), true);
weaponBodyObject.AddComponent<SpriteRenderer>();
weaponBodyObject.GetComponent<SpriteRenderer>().sprite = weaponBodySprites[0];
weaponBodyObject.AddComponent<Rigidbody2D>();
weaponBodyObject.AddComponent<PolygonCollider2D>();
weaponBodyObject.GetComponent<PolygonCollider2D>().sharedMaterial = physicsMaterial;
weaponSpriteObject.AddComponent<SpriteRenderer>();
weaponSpriteObject.GetComponent<SpriteRenderer>().sprite = weaponSprites[0];
//weaponSpriteObject.AddComponent<Rigidbody2D>();
weaponBodyObject.SetActive(false);
weaponSpriteObject.SetActive(false);
weaponSpriteObject.transform.parent = weaponBodyObject.transform;
Physics2D.IgnoreCollision(weaponBodyObject.GetComponent<PolygonCollider2D>(), weaponBodyObject.GetComponent<PolygonCollider2D>(), true);
weaponColliders = new PolygonCollider2D[16];
for (int i = 0; i < 16; i++)
{
weaponSpriteObject.GetComponent<SpriteRenderer>().sprite = weaponSprites[i];
PolygonCollider2D collider = weaponSpriteObject.AddComponent<PolygonCollider2D>();
collider.sharedMaterial = physicsMaterial;
weaponColliders[i] = collider;
}
}
public void Update()
{
if (attacking == true)
{
ContinueAttack();
}
}
public void UpdateRotation(int newAngle)
{
bodySpriteObject.transform.rotation = Quaternion.Euler(0f, 0f, newAngle);
//bodyWeaponObject.transform.rotation = Quaternion.Euler(0f, 0f, newAngle);
}
public void UpdatePosition(Vector3 newPositionVector)
{
if (moving == true)
{
bodySpriteObject.GetComponent<Rigidbody2D>().velocity = new Vector3();
bodySpriteObject.transform.position += newPositionVector * 1 * Time.deltaTime;
}
//bodyWeaponObject.GetComponent<Rigidbody2D>().velocity = new Vector3();
//bodyWeaponObject.transform.position += newPositionVector * 1 * Time.deltaTime;
}
public void UpdateSprite(int spriteIndex)
{
bodySpriteObject.GetComponent<SpriteRenderer>().sprite = bodySprites[spriteIndex];
//bodyWeaponObject.GetComponent<SpriteRenderer>().sprite = bodyWeaponSprites[spriteIndex];
}
public void OnAttack()
{
attacking = true;
bodyWeaponObject.SetActive(false);
bodySpriteObject.SetActive(false);
weaponBodyObject.transform.localRotation = bodySpriteObject.transform.localRotation;
weaponBodyObject.transform.position = bodySpriteObject.transform.position;
weaponSpriteObject.SetActive(true);
weaponBodyObject.SetActive(true);
attackIndex = 0;
}
public void ContinueAttack()
{
weaponSpriteObject.GetComponent<SpriteRenderer>().sprite = weaponSprites[(int)attackIndex%16];
weaponBodyObject.GetComponent<SpriteRenderer>().sprite = weaponBodySprites[(int)attackIndex%16];
for (int i = 0; i < weaponColliders.Length; i++)
{
if (i == (int)attackIndex % 16)
weaponColliders[i].enabled = true;
else weaponColliders[i].enabled = false;
}
attackIndex += 0.2f;
}
public void EndAttack()
{
Debug.Log("Ending Attack");
attacking = false;
bodyWeaponObject.SetActive(true);
bodySpriteObject.SetActive(true);
weaponSpriteObject.SetActive(false);
weaponBodyObject.SetActive(false);
}
public void EndMovement()
{
moving = false;
}
ok so apparently this was fixed in 2014 in unity 4.6?
doesn't seem to be fixed at all. i came across this solution though: https://answers.unity.com/questions/1093169/does-polygoncollider2doverlappoint-perform-differe.html
nope. but updating to Unity 2018 seems to have fixed this.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162614im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move BoxCollider2D 1 Answer
Unity 2d Physics Collision Jumping / Glitching 0 Answers
Platformer2D Player sticks to platform corners 0 Answers
Tile Generation with collider2D's 1 Answer