0
\$\begingroup\$

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?

\$\endgroup\$
1
\$\begingroup\$

The problem is with you using Rect box = this.guiText.GetScreenRect (); it returns size in pixels you need to convert it into unity3d units.

\$\endgroup\$
  • \$\begingroup\$ I have been looking and there does not seem to be an easy way to go from Pixels to Units. Do you know of a method to do this? \$\endgroup\$ – Xaxum Sep 24 '14 at 19:18
  • \$\begingroup\$ @Xaxum stackoverflow.com/questions/22594074/… \$\endgroup\$ – 2600th Sep 24 '14 at 19:46
  • \$\begingroup\$ Thanks. I did see that though not exactly clear. I found this and it seems address the conversion direclty. answers.unity3d.com/questions/36678/… However, still have the issue. If I drag the guitext way out of the screen and then back in it triggers. So something is larger than it is. I don't have this issue with 3d objects, they just work. \$\endgroup\$ – Xaxum Sep 24 '14 at 20:26
  • \$\begingroup\$ Why are you not initializing rect by some default size or Instantiating it from a prefab? And the answer I shared explains exactly the mathematics to convert pixels to units depending on any configuration and it's simple mathematics. \$\endgroup\$ – 2600th Sep 25 '14 at 5:39
  • \$\begingroup\$ I updated my code based on this answer and it solved an issue I was not aware of. But it still triggers over a large part of the screen, more so on the left. \$\endgroup\$ – Xaxum Sep 25 '14 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.