Change CapsuleCollider2D to match width of paddle

Hi to all, I’m new to C# and unity and I was wondering if anyone can help me out.

I’m using a 3 part paddle (leftend-middle-rightend)

However there is some bad scaling on the CapsuleCollider2d when setting its width to match the total paddle width.

I have a script attached to the middle part of my paddle which has this method:

void SetColliderWidth()
     float xScale = this.transform.localScale.x;
     float leftEnd = 0.32f; 
     float rightEnd = 0.32f;
     float padWidth = 0.08f * xScale;
     float newCapCollWidth = leftEnd + rightEnd + padWidth;
     this.GetComponent<CapsuleCollider2D>().size = new Vector2 (newCapCollWidth, 0.41f);

As I understand things:

The width of the middle section is 8px and my pixels per unit is 100 so the width of the middle in unity is 0.08, and its transform.localScale.x = 1. If I change the scale of the middle to 2 the middle part doubles in width and both end pieces adjust to the new size, however the collider width shows the correct size but is way bigger than it should be:

This image is with scale set to 1 and the collider.size.x is the right width i.e: 0.32 + 0.08 + 0.32 = 0.72

This image is with scale set to 2, the numbers add up :(0.32 + 0.08 * 2 + 0.32 = 0.8)
but the collider.size.x is way bigger than it should be

Any help on this would be really great.

Privacy & Terms