Pookie
Pookie

Reputation: 1279

Scaling Box Collider to Image in Canvas in Unity

I have two Paddle's rendered with SpriteRenderer's. They have BoxCollider's attached to them and they work perfectly. However, they don't scale correctly with different resolutions. So I switched to using Image's in a canvas. They scale perfectly. However, I can't seem to get the BoxCollider to scale correctly with the Paddle's. I have searched the internet and found a few solutions, but they were completely wrong. Here was my conclusion based on the majority of what I saw:

 GetComponent<BoxCollider2D>().size = gameObject.GetComponent<RectTransform>().sizeDelta;

This didn't work at all.

Here is an image of what I need in case it isn't clear.

enter image description here

The green box is the correct size in that image but that isn't what happens, that is what I need to happen.

So, essentially I need a way to scale the BoxCollider2D to the size of the Image in the Canvas.

Here is an image of the inspectors of Paddle and Canvas(Note: Paddle has the script, I just cut it out since it doesn't work anyway):

enter image description here

Also note: The reason I need a collider is because I am checking for collision between two GameObjects, the ball and the paddle. This is Pong.

Upvotes: 2

Views: 9524

Answers (1)

Programmer
Programmer

Reputation: 125275

Since you switched from Sprite to Image. You have to remove the Box Collider 2D from your Image. Images don't need Colliders to work. Just make sure that the Image has Image Script attached to it and make sure that Raycast Target is selected.

Remove the code below.

GetComponent<BoxCollider2D>().size = gameObject.GetComponent<RectTransform>().sizeDelta;

You now have to change your game logic code. All your Sprite click detection code must be changed from #1 to #2.

SPRITE VS IMAGE/RAWIMAGE CLICK DETECTION

1.If the Object you are trying to detect touch with is an Image/Canvas, then this is not how to do this. To detect touch with Image/Canvas, you use have to derive from IPointerDownHandler or IPointerClickHandler then implement the functions from them.

public class YourClass : MonoBehaviour,IPointerDownHandler,IPointerClickHandler
{
   public void OnPointerClick(PointerEventData eventData)
   {
      Debug.Log("Clicked");
   }

   public void OnPointerDown(PointerEventData eventData)
   {
      Debug.Log("Down");
   }

}

2.Now if the GameObject you want to detect the touch with is just a 2D Texture or Sprite then use the code below:

if (Input.GetMouseButtonDown(0))
{
    Vector2 cubeRay = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    RaycastHit2D cubeHit = Physics2D.Raycast(cubeRay, Vector2.zero);

    if (cubeHit)
    {
        Debug.Log("We hit " + cubeHit.collider.name);
    }
}

For this to work, you must attach Collider2D to the 2D Texture or Sprite. Make sure that the Collider is covering the 2D Texture or Sprite by re-sizing the collider. Since this is a 2D game, any collider you are using must end with 2D.For example, there is a Box Collider and there is a Box Collider 2D. You must attach Box Collider 2D. to the Sprite/Texture.

Upvotes: 5

Related Questions