# How to properly squeeze an object when is being pressed between two others based on percentage of scale?

Am trying to do this: How can I "squeeze" a game object when it is pressed between two others?

Which has anwsers, but with some issues, like not lerping back to original scale or not resizing properly based on percentage of collisions (what am struggling with). So this is how I´ve solved it until now:

``private Vector3 originalScale; private float currentX; private float currentZ; private float area; public float minValue = 0.6f; //1 is perfect horizontal/vertical MeshFilter filter;  public float skinWidth = 0.1f; public float firstLenght = 0.2f; //must be grater than skniWidth;  RaycastHit hit;  GameObject leftEmpty; GameObject rightEmpty;  bool pressedLeft = false; bool pressedRight = false;  float diffX; float originalDiffX;  bool orig = false;  void Awake() {     filter = this.GetComponent<MeshFilter>(); }  void Start() {     originalScale = new Vector3(filter.mesh.bounds.size.x, filter.mesh.bounds.size.y, filter.mesh.bounds.size.z);     area = originalScale.x * originalScale.z;     leftEmpty = new GameObject();     rightEmpty = new GameObject(); }  void OnCollisionStay(Collision coll) {     if (coll.gameObject.tag != "Ground" && coll.gameObject.tag != "Ceiling")     {         //if the collision is from the left side         for (int i = 0; i < coll.contacts.Length; i++)         {             if (coll.contacts[i].normal.x > minValue) //am checking direction based on normals, not positions              {                 Vector3 rayOrigin = coll.contacts[i].point + Vector3.right * skinWidth;                 if (pressedLeft == false)                 {                     if (Physics.Raycast(rayOrigin, -Vector3.right, out hit, firstLenght))                     {                         Debug.DrawRay(rayOrigin, -Vector3.right * firstLenght, Color.blue, 0.1f);                         //Am doing all of this to take the contact points only when they are right (before colliding they are wrong). So the contact point is in the right place only if...                         if (hit.distance <= skinWidth + 0.00008f) // last number is to compensate for weird physics, just ignore it                         {                             leftEmpty.transform.position = hit.point;                             pressedLeft = true;//so the leftEmpty´s position is set only once, then is moved via parenting below                         }                     }                 }                  leftEmpty.transform.SetParent(coll.transform);//am parenting them so I can keep them after OnCollisionExit because I´ll use this numbers also to lerp back to original scale             }         }         //if the collision is from 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 (pressedRight == false)                 {                     if (Physics.Raycast(rayOrigin, Vector3.right, out hit, firstLenght))                     {                         if (hit.distance <= skinWidth + 0.00008f)                          {                             rightEmpty.transform.position = hit.point;                             pressedRight = true;                         }                     }                 }                  rightEmpty.transform.SetParent(coll.transform);             }         }     }       if (pressedLeft == true && pressedRight == 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);     //originally, I was doing this:     currentX = diffX;     currentZ = area / 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 separate points of the mesh (because the x scale was always the distance between collisions)      //That´s why I wanted percentage, to scale based on the hit points and not the complete width, so I did this:     currentX = originalScale.x / (originalDiffX / diffX); // or (originalScale.x / originalDiffX) * diffX; anything works.     //However, while it solves the percentage issue, it only does it if the collisions stay at the original collision points, which won´t be the case.     //then I tried this to get the originalDiffX equivalent of the updated diffX:     Vector3 closestLeft = this.GetComponent<Collider>().ClosestPoint(leftEmpty.transform.position);     Vector3 closestRight = this.GetComponent<Collider>().ClosestPoint(rightEmpty.transform.position);      float distX = Mathf.Abs(closestRight.x - closestLeft.x);     float mult = (originalDiffX / diffX) * distX;      currentX = originalScale.x / (mult / diffX); //the same formula but replacing the originalDiffX by "mult". I didn´t work at all. The object started to vibrate weirdly.   } ``

And I want to improve this: currentX = originalScale.x / (originalDiffX / diffX); As you can see, it goes through; but it doesn´t if the collisions stay at the original collision points:  This is 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 the original x scale. That 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 be that only 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 stay at those corresponding points; But because the object itself is being squeezed, or moving differently to colliders, the colliders start touching different places. As seen in the screenshots, they started colliding on the lowest part, but later they were above there.

Posted on Categories Free Proxy