Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by pretender · Sep 05, 2011 at 05:08 PM · androidiphonezoompinch

easiest way to do pinch zoom?

does somebody have example of how to do pinch zoom like in iphone? i am developing for android and currently i have zoom in/out with two fingers up/down and i would like to change that.

thank you!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by mcarriere · Sep 08, 2011 at 03:14 PM

Check the distance between two touches, and move your camera accordingly.

 if (Input.touchCount >= 2)
 {
     Vector2 touch0, touch1;
     float distance;
     touch0 = Input.GetTouch(0).position;
     touch1 = Input.GetTouch(1).position;

     distance = Vector2.Distance(touch0, touch1);
 }

From here you could clamp your distance to a min/max distance, and in turn, use that to change the position or FOV of your camera.

If you do a direct mapping, it means that you're going to have two fingers close together always mean "zoomed in", and vice versa for two fingers far apart.

You could also save the last distance in a class variable, and just do something on the delta between the last saved distance, and the current distance change.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
14

Answer by 1337GameDev · Nov 02, 2011 at 01:33 AM

You are missing key points. Here is a step by step on how to do it: 1: if statement to check # if fingers touching and if they are in the moved phase 2: within the if, figure out the distance between the two points current position 3: within the if calculate the previous position by subtracting the current positions with the positionDelta (the distance each touch moved) 4: then within the if, declare a new if based on weather positions of each touch moved farther apart by subtracting the distance each touch moved from previous to current update (using position and previous position.positionDelta respectively). Separate into two if statements where one checks if one distance is larger and one is smaller to see if it was an inward or outward pinch gesture. 5. Apply your actions under each if statement respectively and add mathf.clamp(input float, min float, max float) (float is data type) to limit the action bounds or a speed mod here if wanted.

using UnityEngine; using System.Collections;

public class CameraZoomPinch : MonoBehaviour { public int speed = 4; public Camera selectedCamera; public float MINSCALE = 2.0F; public float MAXSCALE = 5.0F; public float minPinchSpeed = 5.0F; public float varianceInDistances = 5.0F; private float touchDelta = 0.0F; private Vector2 prevDist = new Vector2(0,0); private Vector2 curDist = new Vector2(0,0); private float speedTouch0 = 0.0F; private float speedTouch1 = 0.0F;

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

 if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) 
 {

     curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
     prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
     touchDelta = curDist.magnitude - prevDist.magnitude;
     speedTouch0 = Input.GetTouch(0).deltaPosition.magnitude / Input.GetTouch(0).deltaTime;
     speedTouch1 = Input.GetTouch(1).deltaPosition.magnitude / Input.GetTouch(1).deltaTime;


     if ((touchDelta + varianceInDistances <= 1) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed))
     {

         selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
     }

     if ((touchDelta +varianceInDistances > 1) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed))
     {

         selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
     }

 }       

}

}

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image exbow · Mar 23, 2012 at 05:03 PM 1
Share

thank you so much about that m8. Great help

avatar image exbow · Mar 23, 2012 at 08:02 PM 0
Share

Really thanks for that m8. works like a charm

avatar image samz · May 08, 2012 at 12:58 AM 0
Share

wth it dont work for me, the camera doesnt zoom in or out??

avatar image sanks007 · Sep 03, 2013 at 09:25 AM 0
Share

hey this script didn't worked for me as well.. :( Have attached this script on main camera (orthographic).can you tell me whats the issue ?

avatar image 1337GameDev · Sep 03, 2013 at 03:20 PM 0
Share

You have to assign the selectedCamera variable in the inspector, or find the camera in start and assign it via script.

Show more comments
avatar image
4

Answer by timebenter · Mar 17, 2021 at 10:54 PM

Here is some nice and simple code. The variable touchDist is the change in distance between your two fingers.

 float touchDist = 0;
 float lastDist = 0;
 
 void Update()
 {
     if (Input.touchCount == 2)
     {
         Touch touch1 = Input.GetTouch(0);
         Touch touch2 = Input.GetTouch(1);
 
         if (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Began)
         {
             lastDist = Vector2.Distance(touch1.position, touch2.position);
         }
 
         if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved)
         {
             float newDist = Vector2.Distance(touch1.position, touch2.position);
             touchDist = lastDist - newDist;
             lastDist = newDist;
 
             // Your Code Here
             Camera.main.fieldOfView += touchDist * 0.1f;
         }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Helix_abdu · May 08, 2015 at 10:26 PM

this is a good tutorial from unity-tutorials for beginners https://unity3d.com/learn/tutorials/modules/beginner/platform-specific/pinch-zoom

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by AngryGamemaster · Dec 06, 2019 at 10:17 PM

im using a camera set to orthographic in a 3d space. so i can go between 3d isometric and straight on like a platformer. so ill include my code i use. after seeing that touch information.

 using UnityEngine;
 
 namespace Version3
 {
     public class PinchZoom : MonoBehaviour
     {
         // orthographic sizes
         public static float MaxZoom = 6f;
         public static float StdZoom = 1.44f;
         public static float MinZoom = 0.1f;
 
 
         public static PinchZoom HANDLE;
 
         public Camera targetCamera;
         public float initialOrthoSize;
 
 
         void Awake(){
             HANDLE = this;
             initialOrthoSize = targetCamera.orthographicSize;
         }
 
         public void ChangeCamera(Camera newTargetCamera){
             float newInitialSize = newTargetCamera.orthographicSize;
             newTargetCamera.orthographicSize = targetCamera.orthographicSize;
             targetCamera.orthographicSize = initialOrthoSize;
             initialOrthoSize = newInitialSize;
         }
 
         public float panSpeedAdjustment = 1.0f;
 
         public float currentOrthoSize = 0f;
 
         public float lastDistance = 0f;
 
 
         void Update()
         {
             if (Input.touchCount >= 2) 
             {
                 //twoTouches = true;
                 Vector2 touch0, touch1;
                 float distance;
                 touch0 = Input.GetTouch (0).position;
                 touch1 = Input.GetTouch (1).position;
                 distance = Vector2.Distance (touch0, touch1);
                 if (lastDistance == 0f) 
                 {
                     lastDistance = distance;
                     currentOrthoSize = targetCamera.orthographicSize;
                 } 
                 else 
                 {
                     if (distance > lastDistance) 
                     {
                         // zoom in
                         float multiplier = lastDistance/distance;
                         // 1/1.2 = .833
                         float newOrthoSize = currentOrthoSize * multiplier;
                         if (newOrthoSize >= MinZoom) 
                         {
                             targetCamera.orthographicSize = newOrthoSize;
                             AdjustPanSpeed (newOrthoSize);
                         } 
                         else 
                         {
                             targetCamera.orthographicSize = MinZoom;
                             AdjustPanSpeed (newOrthoSize);
                         }
                     } 
                     else 
                     {
                         // zoom out max 6 float
                         float multiplier = lastDistance / distance;
                         // 1.2/1 = 1.2
                         float newOrthoSize = currentOrthoSize * multiplier;
                         if (newOrthoSize <= MaxZoom) 
                         {
                             targetCamera.orthographicSize = newOrthoSize;
                             AdjustPanSpeed (newOrthoSize);
                         } 
                         else 
                         {
                             targetCamera.orthographicSize = MaxZoom;
                             AdjustPanSpeed (newOrthoSize);
                         }
                     }
                 }
             } else {
                 lastDistance = 0f;
                 currentOrthoSize = 0f;
                 AdjustPanSpeed (targetCamera.orthographicSize);
             }
         }
 
         // Exists for other zoom changing functions in other places.
         public void AdjustPanSpeed(float orthographicSize){
             if (orthographicSize <= StdZoom) {
                 panSpeedAdjustment = orthographicSize/StdZoom; // slows down panning
             } else {
                 panSpeedAdjustment = 1.0f; // no speed change
             }
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to zoom and pan when you "pinch/swipe across screen" (iPhone) 5 Answers

Pinch-zoom camera 2 Answers

Mobile Zoom System, That Zooms In On The Right Position 1 Answer

How to centre a 3D pinch zoom 1 Answer

Unified cross-platform project? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges