# Zoom to Center of Pinch

Updated on October 7, 2018 in
0 on October 7, 2018

Hi,

I am working on Tower Defense game with touch controls like Clash Of Clans.

I can zoom to center of screen without problem.

You know that’s simple algorithm:

1- Take positions of finger1 and finger2.

2- Take the average positions of finger1 and finger2.

3- Use ScreenToWorldPoint.

4- LookAt worldPoint.

So What’s wrong ? am i create bad algorithm is there missing something that i don’t know ?

I added Camera Settings on Unity Inspector this is my scene camera:

GameView:

Here is my code:

```

case 2:
Vector2[] newPositions = new Vector2[] { Input.GetTouch(0).position, Input.GetTouch(1).position };
if (!wasZoomingLastFrame)
{
lastZoomPositions = newPositions;
wasZoomingLastFrame = true;
}
else
{

float newDistance = Vector2.Distance(newPositions[0], newPositions[1]);
float oldDistance = Vector2.Distance(lastZoomPositions[0], lastZoomPositions[1]);
float offset = newDistance - oldDistance;

// Vector2 midPoint = (newPositions[0] + newPositions[1]) / 2f; Center of Finger1 and 2
// Vector3 worldPoint = new Vector3(midPoint.x, midPoint.y, cam.nearClipPlane);
// worldPoint = cam.ScreenToWorldPoint(worldPoint);
ZoomCamera(offset, ZoomSpeedTouch, worldPoint);

lastZoomPositions = newPositions;
}
break;

default:
wasZoomingLastFrame = false;
break;
}
}

```

``` void ZoomCamera(float offset, float speed,Vector3 look)
{
if (offset == 0)
{
return;
}
// cam.transform.LookAt(look); Trying to Look at Center of Pinch
cam.fieldOfView = Mathf.Clamp(cam.fieldOfView - (offset * speed), ZoomBounds[0], ZoomBounds[1]);
}
```
• Liked by