I'm trying to do this: how can I "pinch" an object from the game when it is pressed between two others?

Which has some answers, but with some problems, like not going back to the original scale or resizing correctly according to the percentage of collisions (which is a problem for me). Here's how I solved it so far:

```
private Vector3 originalScale;
private float currentX;
private float currentZ;
private floating area;
floating public minValue = 0.6f;
// 1 is perfect horizontal / vertical
MeshFilter filter;
float public skinWidth = 0.1f;
public float firstLenght = 0.2f;
// must be greater than skniWidth;
RaycastHit hit;
GameObject leftEmpty;
GameObject rightEmpty;
bool pressureLeft = false;
bool pressureRight = false;
float diffX;
float originalDiffX;
bool orig = false;
empty Wake up ()
{
filter = this.GetComponent();
}
empty Start ()
{
originalScale = new Vector3 (filter.mesh.bounds.size.x, filter.mesh.bound.size.y, filter.mesh.bounds.size.z);
area = originalScale.x * originalScale.z;
leftEmpty = new GameObject ();
rightEmpty = new GameObject ();
}
Cancel OnCollisionStay (Collision Coll)
{
if (coll.gameObject.tag! = "Ground" && coll.gameObject.tag! = "Ceiling")
{
// if the collision is on the left side
for (int i = 0; i < coll.contacts.Length; i++)
{
if (coll.contacts[i].normal.x > minValue) // check the direction according to normals, not positions
{
Vector3 rayOrigin = coll.contacts[i].point + Vector3.right * skinWidth;
if (press the left == false)
{
if (Physics.Raycast (rayOrigin, -Vector3.right, out hit, firstLenght))
{
Debug.DrawRay (rayOrigin, -Vector3.right * firstLenght, Color.blue, 0.1f);
// I do all this to take contact points only when they are right (before colliding, they are wrong). So, the point of contact is in the right place as if ...
if (hit.distance <= skinWidth + 0.00008f) // the last number is used to compensate for a strange physics, just ignore it
{
leftEmpty.transform.position = hit.point;
pressureLeft = true; // the leftEmpty position is only defined once, then moved via parenting underneath
}
}
}
leftEmpty.transform.SetParent (coll.transform); // am their parent so that I can keep them after OnCollisionExit because I also use these numbers to return to the original scale
}
}
// if the collision is on the right side
for (int i = 0; i <coll.contacts.Length; i ++)
{
if (coll.contacts[i].normal.x <- (minValue))
{
Vector3 rayOrigin = coll.contacts[i].point - Vector3.right * skinWidth;
if (pressed right == false)
{
if (Physics.Raycast (rayOrigin, Vector3.right, out hit, firstLenght))
{
if (hit.distance <= skinWidth + 0.00008f)
{
rightEmpty.transform.position = hit.point;
pressureRight = true;
}
}
}
rightEmpty.transform.SetParent (coll.transform);
}
}
}
if (coloredLeft == true && coloredRight == true && orig == false)
{
originalDiffX = Mathf.Abs (rightEmpty.transform.position.x - leftEmpty.transform.position.x);
orig = true;
}
}
void Update ()
{
diffX = Mathf.Abs (rightEmpty.transform.position.x - leftEmpty.transform.position.x);
// I originally did this:
currentX = diffX;
currentZ = zone / currentX;
if (leftEmpty.transform.position.x! = 0 && rightEmpty.transform.position.x! = 0)
{
if (currentX <= originalScale.x)
{
transform.localScale = new Vector3 (currentX / filter.mesh.bounds.size.x, transform.localScale.y, currentZ / filter.mesh.bounds.size.z);
}
}
// And it worked, but only if the collisions were always on the most separated points of the mesh (because the x-scale was always the distance between the collisions)
// This is the reason I wanted a percentage, scaled according to the impact points and not the total width, so I did this:
currentX = originalScale.x / (originalDiffX / diffX); // or (originalScale.x / originalDiffX) * diffX; everything is working.
// However, if this solves the percentage problem, it does so only if the collisions remain at the original collision points, which will not be the case.
// then I tried this to get the original diffX equivalent of the updated diffX:
Vector3 the closestLeft = this.GetComponent() .ClosestPoint (leftEmpty.transform.position);
Vector3 closestRight = this.GetComponent() .ClosestPoint (rightEmpty.transform.position);
float distX = Mathf.Abs (mostRight.x - mostLeft.x);
float mult = (originalDiffX / diffX) * distX;
currentX = originalScale.x / (mult / diffX); // the same formula but replacing the originalDiffX with "mult". I did not work at all. The object began to vibrate strangely.
}
```

And I want to improve this: currentX = originalScale.x / (originalDiffX / diffX);

As you can see, it goes through; but this is not the case if the collisions remain at the collision points of origin:

It's easier to explain with an example. Let's say (originalScale.x / originalDiffX) * diffX = (12/6) * 3 = 6; 6 is the original diff value, which is half of the original scale x. This means that the double of the updated diff (in this case 3) will always be the number I want, the number that will become the X scale. But it will only be if the collisions always follow the original contacts. in the mesh (the numbers that gave me 6). So, this operation will only be correct if the collisions remain at the corresponding points; But because the object itself is compressed or moves differently from colliders, these begin to touch different places. As seen in the first and last screenshot, they started colliding on the lowest part, but later they were above.