- Home /
Pinch zoom
Hello, I am making upgrade of pinch zoom, which will on camera size increasing move camera higher, and on camera size decreasing move camera lower... I tried to do this upgrade but when is display touched by two fingers, the position of camera go automatically higher and ignores any gestures. Please, help me. :)
void Update()
{
// If there are two touches on the device...
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 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;
//===========================================================>>
transform.Translate (new Vector2(0,prevTouchDeltaMag) *moveSpeed);
//<<===========================================================
// If the camera is orthographic...
if (GetComponent<Camera>().orthographic) {
// ... change the orthographic size based on the change in distance between the touches.
GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
// Make sure the orthographic size never drops below zero.
GetComponent<Camera>().orthographicSize = Mathf.Max (GetComponent<Camera>().orthographicSize, 0.1f);
}
}
Have you tried to do something like
float mag = (touchZero.deltaPosition - touchOne.deltaPosition).magnitude;
if(mag > touchZero.deltaPosition.magnitude)
{
//there, the finger are zoo$$anonymous$$g in
}
else
{
//there the fingers are zom$$anonymous$$g out
}
I think that's how I would do it, but i haven't tested it.
also, you should get the component for the camera only once, because 4 time like this is more demanding.
Hope that help you :)
Good luck, FireCube
Answer by srikaran_p · Jul 06, 2016 at 10:00 AM
Watch this video and you will get what you need.
http://unity3d.com/learn/tutorials/topics/mobile-touch/pinch-zoom?playlist=17138
Your answer
Follow this Question
Related Questions
Problem with multi touch[solved] 0 Answers
transformDirection for camera control 1 Answer
Multiple Cars not working 1 Answer
Lerp/Slerp making the Camera get stuck,Lerp / Slerp getting stuck 0 Answers
RTS touch camera movement 1 Answer