Scale a 3D object by preserving the aspect ratio

Hello,

Scenario
I have a 3D object when I would like to scale along x & y (local) when user pinches. Now this 3D object might be in 1:1 ratio or 16:9 ratio. I have no control over it. When user pinches, I want to scale appropriately.
Pinch delta values are very small. Somewhere between 0.1 to -0.1. Hence it is not about the magnitude.

Existing Code

scaleMagnitude = new Vector2
                                         (
                                             scaleMagnitude.x + _value,
                                             scaleMagnitude.y + _value
                                          );

this.transform.localScale = new Vector3(scaleMagnitude.x, scaleMagnitude.y, 1);

Expectation
I add the same values over x & y which make me believe - it is not being scaled by maintaining it’s aspect ratio.

Request

  • How do I do it?
  • How do I cap a min & max to clamp the scaling?

Thank you,
Karsnen

I may be wrong, but you would want to multiply X and Y scale by the same value?

  • Get the distance between the two finger touches on start, store that in one variable like distStart.
  • Store the scale of the object on initial touch, into a Vector like startScale.
  • then the current distance between two finger touches, store that in one variable like distCurrent.
  • Then make a variable like scaleAmount = distCurrent - distStart;

Then have a function for while you are pinching the screen and make
transform.scale = new vector2(startScalex * scaleAmount.x, startScale.y * scaleAmount.y)

You could set a min/max scale value in a float. Then do something like…
transform.scale = new vector2(mathF.clamp(startScalex * scaleAmount.x, min, max), …);

Or are you trying to do something else?

1 Like