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.
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):
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.