Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by AtomicMarine · Nov 19, 2011 at 11:04 PM · javascriptiosiphonezoom

Zooming in and out on iPhone

What is the theory behind the pinch zoom in and out technique used on the iPhone?? How would one do this? Do you get the distance between 2 touch points and measure them? can someone point me in the right direction :)

Thanks :P

Here is the end code, it works for zooming in and out as well as moving around the iPhone, good usage in IOS games

 //Author DOMINIC OBOJKOVITS
 var check : boolean = false;
 var OPinchDist : float;
 var currentDist : float;
 var counter : float ;
 var touchP : Vector2;
 var thing : Transform;
 var speed : float = 0.1;
 var Osize : float;
 
 function Start(){
     
     iPhoneKeyboard.autorotateToPortrait = false; 
     iPhoneKeyboard.autorotateToPortraitUpsideDown = false; 
     iPhoneKeyboard.autorotateToLandscapeRight = false; 
     iPhoneKeyboard.autorotateToLandscapeLeft = false;
 
 }
 
 function Update () {
     
     var count = Input.touchCount;
     
     //Limits
     Osize = Camera.main.orthographicSize;
     if (Osize >= 12){
         Osize = 12;    
     }
     if (Osize <= 4){
         Osize = 4;    
     } 
     speed = 0.05 * (Osize/5);
     
     //Zooming Factor
     if (count >= 2){
     
         if (check == false){
             OPinchDist = Vector2.Distance(Input.GetTouch(0).position,Input.GetTouch(1).position);
             check = true;
         }
         currentDist = Vector2.Distance(Input.GetTouch(0).position,Input.GetTouch(1).position);
         var zoomFactor = ( (OPinchDist) / (currentDist) );
     
         Camera.main.orthographicSize = Osize * zoomFactor;
 
     }
     if (count <= 1){
         check = false;    
     }
     
     //Moves the camera around
      if (Input.touchCount == 1 && 
       Input.GetTouch(0).phase == TouchPhase.Moved) {
         var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
         thing.Translate (-touchDeltaPosition.x * speed,-touchDeltaPosition.y * speed, 0);
     }
     
     
 }
Comment
Add comment · Show 1
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 Pursehouse · Dec 14, 2016 at 06:41 AM 0
Share

Thanks for updating the question, but it still seems to be acting weird for me. Basically I put my 2 fingers down, and if I roll (yes roll, not even move ) the touch points closer together, the zoom rushes from wide to narrow. Did you do any fixes to this after? I know it's been ages :) Thanks!

1 Reply

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

Answer by WillTAtl · Nov 20, 2011 at 02:35 AM

Basically, you calcualte and store the distance between touches at the start of the zoom, and compare it to the distance between them to determine the zoom level along the way.

Calculating the distance between two touches is easy...

 var pinchDistance = Vector3.Distance(touchPos1, touchPos2);
 

Then you'll calculate the basic scaling factor something like this...

 var zoomFactor = (currentPinchDistance/originalPinchDistance);

this can be applied in several different ways depending on your project. In a a third-person perspective view, you might use this factor to adjust the distance of the camera from the object. With an orthographic camera, you would apply it to the camera's orthographicSize. In a first-person game for a sniper scope, you'd probably want to apply it to the camera's field of view. In most cases you could probably apply by just multiply it with the original value of those varibles before the pinch zoom started, ex, for orthographic camera...

 Camera.main.orthographicSie = originalOrthoSize * zoomFactor;

Hope this helps!

Comment
Add comment · Show 3 · 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 AtomicMarine · Nov 20, 2011 at 03:25 PM 0
Share

This worked perfectly! Thank you so much, once you showed the code to get the distances between to touches everything else fell into place quite well :D

avatar image rsrhcp · Mar 04, 2012 at 08:53 AM 0
Share

Do$$anonymous$$ic, would you $$anonymous$$d posting that code? For others, it doesn't fall into place just that well :) Thanks!

avatar image AtomicMarine · Mar 04, 2012 at 03:56 PM 0
Share

@rsrhcp Sure thing, ill add it in the question.. :P am working on some amazing game algorithms if you need other stuff

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save data on iOS? 1 Answer

Strongly Typed Javascript on iOS 2 Answers

Magnifying Glass Unity iOS 0 Answers

Help converting javascript to ios iphone scripting 2 Answers

What shader blending modes don't work on iOS? 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