2D Pinch Zoom and Pan in Unity

Hello!  After a long weekend of vector math and linear algebra I finally figured out how to get the zoom look for our new game Zen Balance.  This zoom is the same kind of zooming you see in Maps or Photos apps where the point you are pinching around won't move while you are zooming in and out.  It also allows you to do a two finger pan.  Below is the code I've come up with and it should be easy to implement in any other game.

Important Notes!! - - Please Read!!

A few quick notes before we begin.  

  • This code is for a 2D Game using the Orthographic Projecting mode for the camera.
    • (If you aren't making a 2D game and your camera isn't in Orthographic Projection I have no idea what this code will do).
  • I did not write the entirety of the following script.  It contains code taken from the Unity Learn website from the following URL: https://unity3d.com/learn/tutorials/modules/beginner/platform-specific/pinch-zoom

So with that out of our way, here's the FILE DOWNLOAD:

How To Implement

To implement the code in your project simply create a new C# script with the previous code and attach it to the camera you want to have zoom capabilities.  That's it.  Easy.  You can adjust the public zoom variables to change the speed at which the camera zooms, but I've found the 0.25 works best for us.

I hope this helps you out and if you have any questions feel free to let me know in the comments!


EDIT:  Here is a version of the pinch-zoom script that will not zoom out further than the initial camera size.  It can be DOWNLOADED HERE