• 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
0
Question by sidhant_2996 · Jul 26, 2017 at 07:56 AM · zoom

I need help in zooming an object using double tap in my android device

So in the game scene i have an object (cube), i wanna deploy this in my android phone (just a small demo). so when we double tap the object it should zoom in smoothly, and when we double tap it should zoom out smoothly, can anyone give me some ideas. it would be appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sumit47 · Jan 08, 2018 at 09:07 AM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PinchZoom : MonoBehaviour { public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode. public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode. public float ZoomInMax=80.0f, ZoomInMin=24.0f;

 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 (the distance) 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;

         // If the camera is orthographic...
        // if (camera.isOrthoGraphic)
         if(Camera.main.orthographic)
         {
             // ... change the orthographic size based on the change in distance between the touches.
             Camera.main.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;

             // Make sure the orthographic size never drops below zero.
             Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize, 0.1f);
         }
         else
         {
             // Otherwise change the field of view based on the change in distance between the touches.
            // if (Camera.main.fieldOfView > ZoomInMin && Camera.main.fieldOfView < ZoomInMax)
             //{
                 Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed; 
            // }


             // Clamp the field of view to make sure it's between 0 and 180.
             Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, 24.0f, 80.0f);
         }
     }
     // -------------------Code for Zooming Out------------
     if (Input.GetAxis("Mouse ScrollWheel") < 0)
     {
         if (Camera.main.fieldOfView<=ZoomInMax)
             Camera.main.fieldOfView +=2;
         if (Camera.main.orthographicSize<=20)
             Camera.main.orthographicSize +=0.5f;

     }
     // ---------------Code for Zooming In------------------------
     if (Input.GetAxis("Mouse ScrollWheel") > 0)
     {
         if (Camera.main.fieldOfView>ZoomInMin)
             Camera.main.fieldOfView -=2;
         if (Camera.main.orthographicSize>=1)
             Camera.main.orthographicSize -=0.5f;
     }

     // -------Code to switch camera between Perspective and Orthographic--------
     if (Input.GetKeyUp(KeyCode.B ))
     {
         if (Camera.main.orthographic==true)
             Camera.main.orthographic=false;
         else
             Camera.main.orthographic=true;
     }
 }

}

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 Ali_Jaffer · Jul 26, 2017 at 09:53 AM

the following code i took from another post. I hope this can help

    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
              {
                  tapCount++;
              }
              if (tapCount > 0)
              {
                  doubleTapTimer += Time.deltaTime;
              }
              if (tapCount >= 2)
              {
                  //Decrease camera filed of view code here
                  doubleTapTimer = 0.0f;
                  tapCount = 0;
              }
              if (doubleTapTimer > 0.5f)
              {
                  doubleTapTimer = 0f;
                  tapCount = 0;
              }
 
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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 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 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 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 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

using objects bounds to zoom in object 2 Answers

Could someone write me a 'zoom' script? 3 Answers

Zooming camera using move z position. 0 Answers

GUI button adjustments 2 Answers

audio clip to object and object zoom 0 Answers


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