unity 2d: scaling sprites problem.

96355-screenshot-88.png

Above is my game. Dark blue rectangle is a room - it is made up of one empty game object as a parent and 4 children sprites that are separate lines with their own 2d box colliders.

I want my room to shrink either vertically or horizontally or both when I play the game as time passes by. The problem is that when I shrink the room the parent seen in the hierarchy above, the lines that don’t “shorten” become squashed and look practically invisible. I want all sides to retain their thickness.
96357-screenshot-90.png

When I shrink the two lines seperately without scaling the parent. The thickness is retained but the two other lines do not shift up and join the shortened lines and it no longer looks like a box.

Script I used to shrink the room:

public class shrinkScript : MonoBehaviour {
    public const float timer = 10f; //fixed value for how long it must be counting down. helps reset timer.
    public float countDown; //time that changes
    public float shrinkRateY; //speed at which it shrinks (vertical length of room)

    float RoomY;

    bool gameOver;

    // Use this for initialization
    void Awake() {
         RoomY = transform.localScale.y;

        countDown = timer;

        shrinkRateY = (RoomY / countDown) * Time.deltaTime;
    }

	// Update is called once per frame
	void Update () {
        if (countDown > 0) {
            countDown -= Time.deltaTime;

            float scaleY;  //stored the scale of the gameObject's x  co-ordinate in a variable
            scaleY = Mathf.Clamp(RoomY, 0.05f, 0.9943201f);
            transform.localScale -= new Vector3(0, shrinkRateY,0);
        }
    }
}

Is there any possible way to achieve this?

PS: I want the room to shrink in-game with a script attached and allow the two other sides whose scale isn’t being changed to move up.

Many thanks,

I’d just antiproportionally scale the colliders below your transform.localScale = line.

At the top:

public Transform top;
public Transform bottom;
public Transform left;
public Transform right;

And below your current line 26, with scale being transform.localScale:

SetVerticalScale(top, scale.y);
SetVerticalScale(bottom, scale.y);
SetHorizontalScale(left, scale.x);
SetHorizontalScale(right, scale.x);

SetVerticalScale and SetHorizontalScale are defined like this:

public void SetVerticalScale(Transform t, float parentScale)
{
  var scale = t.localScale;
  scale.y = 1f/parentScale;
  t.localScale = scale;
}

public void SetHorizontalScale(Transform t, float parentScale)
{
  var scale = t.localScale;
  scale.x = 1f/parentScale;
  t.localScale = scale;
}