Pinch to Zoom through Unity3D on iOS

I’ve just started with Unity3D, with the hope that it’ll bring an extra dimension (quite literally) to the apps we develop for our clients.

The 3D Engine allows export directly to XCode, meaning I can use Unity instead of XCode to complete the 3D modelling, coding, controls and interface and then just build it to XCode to test.

One hurdle that I hit in building my application is that there was limited support for iOS coding, i.e. detecting touches instead of mouse clicks. So I set about making my own solutions, some of which were pretty botched jobs.

The lack of sample code is enhanced further by the ability to code in three languages – a Unity derivative of JavaScript, C# or Boo code. So some solutions are perfect for the functionality I’m trying to implement but unfortunately are written in C# whereas I prefer JavaScript.

This means that Unity’s language compatibility can sometimes be a large drawback.

As a new user, it can sometimes be overwhelming to find so many different solutions in so many different codebases to match a single functionality, we could find some code but the original poster may not have specified which code it’s in and new users aren’t at the stage where they are able to identify the languages (purely because the JavaScript is written to support Unity3D’s own proprietary methods, and the C# is the same).

To that end, here is a Pinch to Zoom code I found (and modified to my own needs). I hope somebody else finds it useful.

Pinching to Zoom

I modified the Mouse Orbit script which comes packaged with Unity3D to achieve this.

Within the LateUpdate() code, I began to test for my touches.

The key to this method is creating a delta, or a distance a touch has moved between each frame. Once I have this delta (a single value for each frame derived from a calculation of both finger movements), this can be subtracted or added to a distance variable before the frame is re-rendered.

First however, I need to decide how many fingers have touched the screen. For a pinch gesture, this needs to be two:

Next I need to store the current touch positions:

Now for a little mathematics. There is a method that Unity allows you to use called the deltaPosition, this is designed to save everyone a lot of time. It calculates the touch position difference between the current frame and the previous. We are going to use this to calculate the previous position.

Our delta can now be calculated quite simply, using Unity’s magnitude method. Magnitude is quite simply the length of the vector is square root of (x*x+y*y).

I divide by 50 here because I found that the delta was too high, and I wanted the pinch transition to be slower and cover less of a zoom, this depends entirely on your use though.

Now for the most important line. The line that sets the new distance for the zoom level.

Not Forgetting…

In my code, I wanted to stop the user from zooming too much i.e. clamping the distance between two separate values, a max and a min. Here is what I used:

If the distance is over 10, set it to 10, if it’s under 3.5, set it to 3.5. Simple.

As long as this code runs before my update transformation code then this will work fine; this is packaged with the Mouse Orbit script as standard. Here is my update transformation code for reference:

The Full Code

Here is my full LateUpdate method.

The full code is available here: http://pastebin.com/0P5zVGuK

Comments

comments

Powered by Facebook Comments