Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

PolygonCollider2D doesn't cover the sprite in android

Discussion in '2D' started by PersianKiller, Feb 19, 2018.

  1. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    so it's my code

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. public class DROPITEM : MonoBehaviour {
    7. public Sprite sp;
    8. // Use this for initialization
    9. void Start () {
    10.    
    11.      gameObject.AddComponent<SpriteRenderer> ();
    12.      GetComponent<SpriteRenderer> ().sprite=sp;
    13.      gameObject.AddComponent<Rigidbody2D> ();
    14.      GetComponent<Rigidbody2D> ().velocity=new Vector2(5,3);
    15.               gameObject.AddComponent<PolygonCollider2D> ();
    16. }
    17.  
    18. }
    so i have a sprite name sp,it's a gem,when I start the game,it should add some components to the object,(I made it to drop objects), in pc ,every thing works fine just like this video

    https://streamable.com/h68zf

    it adds sprite renderer,rigidbody2D and then PolygonCollider2D,but when I run my game in android,it seems that it adds PolygonCollider2D at first or for some reason the polygoncollider2D component won't cover the sprite !!!! like this video

    https://streamable.com/bqtcx

    how can I fix it???? it also tried to handle it inside a coroutine ,I added polygonCollider2D after 2 secs but the result was the same :).
     
  2. Brightori

    Brightori

    Joined:
    Sep 15, 2017
    Posts:
    64
    i think better to have prefabs and spawn prefabs with collider on board. I dont understand why u try to make it realtime. Coz all this operations very bad for mobile games.
     
  3. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    there are lots of items,I don't want to create a prefab for all of them,i just add a spriteRenderer and choose it's sprite,then add Rigidbody2D and polygonCollider2D to drop the item. also i have tested this game on mobiles,there was no problem
     
  4. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    it seems that when you add a polygonCollider2D to an object in run time,it doesn't cover it.(in android)