# How can I “squeeze” a game object when it is pressed between two others?

I am trying to "squeeze" a game object when it's pressed between two other objects (e.g., "squeezer 1" and "squeezer 2," below) that continue to move towards eachother.

I wish to reduce width of the object so that total area of the game object is maintained while still being between the two squeezers.

How can I achieve this effect?

• I am extremely busy right now. But I can leave a hint for you. Area of square is W x H. where H = 1/W. So new formula is W x 1/W. Get the distance of both squeezers by Vector3.Distance and assign it to X-Scale of the victim object ;). And at the same time assign 1/distance to the Y scale. Hope it helps – Hamza Hasan Dec 16 '15 at 21:09
• And btw this formula I got from your given link, you can deal yourself with these kind of problems – Hamza Hasan Dec 17 '15 at 3:09
• Normally I'd close this question as a duplicate but I think in this case it's not worth the trouble as this version has more information and is generally a better description of the problem. – user1430 Dec 18 '15 at 21:26

This effect is very easy to produce in Unity using a couple of Box Colliders, and some basic math. The effect will ressemble the following: This is a very simple model for this problem. First you need 3 gameobjects added to your scene. Each object will contain a BoxCollider2D and a RigidBody2D with gravity removed.

The blue boxes just have a simple update method to move them in a static direction, for the purpose of the demo.

The orange box, checks if a collision is currently occurring. If it is colliding, Unity automatically stops the objects from overlapping each other, as they already have a BoxCollider2D attached.

The only thing you need to do, is determine if the box is being pushed on both sides. If it is only being pushed on one side, then do nothing. If it is being pushed on both ends at the same time, then you adjust the size of the box, while retaining its total area.

So to start, calculate the total area of your orange box:

public float Area;
bool PushedLeft, PushedRight;
float ForceLeft, ForceRight;

void Start () {
Area = transform.localScale.x * transform.localScale.y;
}


Then, determine if it is being pushed on both ends at the same time:

void OnCollisionStay2D(Collision2D coll) {

//Check all points of contacts occuring on this object
for (int i = 0; i < coll.contacts.Length; i++) {

//Is the collision occuring to the left of box?
if (this.transform.position.x > coll.contacts[i].point.x)
{
//Is it pushing in? or pulling out?
Vector2 diff = coll.contacts[i].point - coll.contacts[i].normal;
if (diff.x < 0)
{
PushedLeft = true;
ForceLeft = 0.1f;
}
}
//collision occuring to the right of box
else if (this.transform.position.x < coll.contacts[i].point.x)
{
//Is it pushing in? or pulling out?
Vector2 diff = coll.contacts[i].point - coll.contacts[i].normal;
if (diff.x < 0)
{
PushedRight = true;
ForceRight = 0.1f;
}
}
}
}


Then, if it is being pushed on both ends, Squeeze it so that it alters its shape:

void Update () {