I have a guitext object which I can drag. I have attached a 2d rigid body. I have also attached the collider with the following code:
void setCollider()
{
Rect box = this.guiText.GetScreenRect ();
BoxCollider2D boxCollider = (BoxCollider2D) gameObject.AddComponent (typeof (BoxCollider2D));
var boxX = box.width;
var boxY = box.height;
Debug.Log ("boxX = " + boxX + "boxY = " + boxY);
boxCollider.size = new Vector2(boxX, boxY);
}
I have a sprite with a collider set as trigger. However, it triggers as soon as the game starts and is not anywhere near the trigger sprite. It will only trigger the one time when it starts. My Trigger on my sprite is as follows:
void OnTriggerEnter2D (Collider2D other){
Debug.Log ("Order Right Arm");
Debug.Log (other.bounds);
}
EDIT: based on answer I have changed the size of the collider to account for the pixel/unit difference:
void setCollider()
{
float pixelRatio = (Camera.main.orthographicSize * 2) / Camera.main.pixelHeight;
Debug.Log ("Pixel Ratio "+pixelRatio);
Rect box = this.guiText.GetScreenRect ();
BoxCollider2D boxCollider = (BoxCollider2D) gameObject.AddComponent (typeof (BoxCollider2D));
float boxX = box.width * pixelRatio;
float boxY = box.height * pixelRatio;
Debug.Log ("boxX = " + boxX + "boxY = " + boxY);
boxCollider.size = new Vector2(boxX, boxY);
}
Any ideas on how I can get this to trigger only when I drag the guitext over the sprite?