- Home /
Pinch to zoom acting weird on Android
I watched an Unity tutorial recently on how to write pinch to zoom script in C#. I was able to make it work just as it is supposed to work on my Samsung Galaxy Tab S 10.5, but for some weird reason it doesn't work as it should on my phone (Sony Xperia Z2).
I also have a panning feature so the player can move the camera, this also works great on the tablet, but not on the phone.
The problem is that the zoom/pan movement is not constant, it slows down and speeds up weirdly even when I move my finger at a constant speed. Here is a video clip to demonstrate what the problem is: https://www.youtube.com/watch?v=fn-YPs0iU40
I tried using FixedUpdate() instead of regular Update(), but there is no difference at all. Here is the zoom script:
public float orthoZoomSpeed = 1.0f;
public float zoomPercentage; //Controls zoom speed
public Camera mainCamera;
void FixedUpdate () {
zoomPercentage = (mainCamera.orthographicSize / 50.0f);
if(Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch (0);
Touch touchOne = Input.GetTouch (1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
mainCamera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed * zoomPercentage;
mainCamera.orthographicSize = Mathf.Max (mainCamera.orthographicSize, 5.0f);
mainCamera.orthographicSize = Mathf.Min (mainCamera.orthographicSize, 50.0f);
}
}
There shouldn't be anything wrong with this script, as it works just fine with the tablet. Does anybody have any idea why it doesn't work correctly on the phone? I can't publish anything with this script if I can't get it to work properly, really annoying problem!
Answer by Xarbrough · Apr 27, 2015 at 09:39 PM
Your code should use regular Update() for this, or else you might lose input, since the Input class polls at Update() rate not FixedUpdate(). Other than that your script looks fine and I don't know of any particular issue with touches on Android. Maybe you can test on another device and see if its a device issue. And of course you should test your script in a demo scene with nothing else going on. There could always be some interference with other code, UI elements or what not.
I see, thanks for the info. I'm quite new to Unity and program$$anonymous$$g so still don't know how all the functions work.
And by the way, the scene in the Youtube clip is an empty scene, there is only the touch script attached to the camera, one sprite (the sun) and nothing else. It's pretty weird how it acts, I tried it with another Xperia Z2 and the same thing happens on both, zoo$$anonymous$$g and panning feels totally unresponsive. I should get my hands on another Android device to test it further I guess, this may very well be some sort of device specific issue.
I did some more testing and it seems the same issue happens on the tablet too, it just wasn't so pro$$anonymous$$ent yesterday with FixedUpdate(), but after I changed back to Update() I noticed it. So this clearly is not a device specific issue.
$$anonymous$$aybe I should try doing the zoo$$anonymous$$g and panning different somehow, but I don't really know how to do that. There is not that much documentation about zoo$$anonymous$$g and panning with touchscreen (weirdly, one would think this is quite common thing developers need).
Well the place where you got the pinch code is about zoo$$anonymous$$g (or did you get it from elsewhere?):
https://unity3d.com/learn/tutorials/modules/beginner/platform-specific/pinch-zoom
Yeah, watched that tutorial yesterday and modified the code a little for my project. It should work, but for some reason it just doesn't... There is other stuff happening in the code too though, maybe that could be the cause?
Here is the full code if someone wants to take a look:
I made a completely clean project and watched yet another tutorial on this. Even after writing the script again in slightly different way the same keeps happening: panning and zoo$$anonymous$$g is really unresponsive on both of my devices... This is driving me crazy.
Here is the code I used in the clean project:
Your answer
Follow this Question
Related Questions
Mobile Zoom System, That Zooms In On The Right Position 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Receipt JSON Split 0 Answers
Unity 2018.2 LWRP blackscreen on build(Android VR) 2 Answers