- Home /
Question by
unity_XY9GfkpHMyaikw · May 27, 2019 at 10:48 AM ·
touchscaleaugmented-realityzoompinch
How to scale objects individual with pinch zoom in ARCore and set a Min/Max scale?
My code: public void _PinchtoZoom() { if (Input.touchCount == 2) { // Store both touches. Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
float pinchAmount = deltaMagnitudeDiff * 0.02f * Time.deltaTime;
ARObject.transform.localScale += new Vector3(pinchAmount, pinchAmount, pinchAmount);
}
}
How do I set a Min/Max scale to the pinch zoom?
Comment
Best Answer
Answer by unity_XY9GfkpHMyaikw · May 28, 2019 at 08:05 AM
Update: This is the solution
public void pinchtoZoomScaleDown() {
Transform Portal = GameObject.FindWithTag("ARObject").transform;
Vector3 newScale = new Vector3();
newScale.x = Mathf.Clamp(Portal.localScale.x - scalingSpeed, min.x, max.x);
newScale.y = Mathf.Clamp(Portal.localScale.y - scalingSpeed, min.y, max.y);
newScale.z = Mathf.Clamp(Portal.localScale.z - scalingSpeed, min.z, max.z);
Portal.transform.localScale = newScale;
}
public void pinchtoZoomScaleUp()
{
Transform Portal = GameObject.FindWithTag("ARObject").transform;
Vector3 newScale = new Vector3();
newScale.x = Mathf.Clamp(Portal.localScale.x + scalingSpeed, min.x, max.x);
newScale.y = Mathf.Clamp(Portal.localScale.y + scalingSpeed, min.y, max.y);
newScale.z = Mathf.Clamp(Portal.localScale.z + scalingSpeed, min.z, max.z);
Portal.transform.localScale = newScale;
}