'Collider not working after changing object scale

In my 2d game, no collider is working after changing it's scale. My Spaceship/Sprite is surrounding with Box collider 2D. Now I want to use border and that's why using Cube and Cylinder object. Where I used Box collider 2D then it's working fine. But when I am changing Cube/Cylinder scale[my intent is to use one cube to cover whole border] then it's not working and my Spaceship go through into that Cube/Cylinder object. I have also tried different type of collider but after scaling no one is working.

I am using a BoxCollider2D on the cube (wall).

I am changing the cube's transform's local y scale in the inspector:

enter image description here



Solution 1:[1]

As I am not getting a proper answer so I have changed my mind to solve the problem in a different way. Here I am using the coordinate axis to check the border. Now my Spaceship doesn't go outside of screen.

public Rigidbody2D player;
private Vector2 screenBounds;
private float playerWidth;
private float playerHeight;

void Start()
{
    player = this.GetComponent<Rigidbody2D>();
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    playerWidth = transform.GetComponent<SpriteRenderer>().bounds.size.x / 2;
    playerHeight = transform.GetComponent<SpriteRenderer>().bounds.size.y / 2;
}

void FixedUpdate()
{
    MovePlayer();
}

public void MovePlayer()
{
    player.velocity = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    this.transform.Translate(Input.GetAxis("Horizontal"), 0, 0);

    Vector3 viewPos = transform.position;
    viewPos.x = Mathf.Clamp(viewPos.x, -screenBounds.x + playerWidth, screenBounds.x - playerWidth);
    viewPos.y = Mathf.Clamp(viewPos.y, -screenBounds.y + playerHeight, screenBounds.y - playerHeight);
    transform.position = viewPos;

}

Solution 2:[2]

It's a bad idea to use Colliders as a component attached to a scaled GameObject, whether the scaling is positive or negative (the latter having even worse results). I've found this time and again.

For BoxCollider (unlike MeshCollider), you have options if you really need to have a scaled object; both of which involve creating a separate, unscaled GameObject with *Collider attached:

  1. Create a container object that is scaled (1,1,1) with BoxCollider on it. Then create your scaled GameObject (the renderable Mesh) as a child therein. This way you get the scaled look and a working collider. Adjust parent's BoxCollider.size as required to fit model.

  2. Create both as siblings in the hierarchy / scene graph. This is a little cheaper on CPU, as you are performing fewer recursive transformations, but it can get messy, scene-wise.

If you're scaling dynamically i.e. in code, then in both of the above cases, as you adjust the scale on the child, you will need to adjust the BoxCollider.size on the parent to match that scale.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2