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 pwkennedy · Aug 18, 2013 at 04:46 PM · positionzoompinchcamera.screentoworld

Zoom to center of Pinch Gesture

Here is a great script for 2 finger pinch = camera zoom + 1 finger swipe for orbit.

Only problem is it zooms by moving camera straight forward in Z no matter where on the screen you are gesturing.

I want to get the center point of the touches and have the zoom move forward to this location (not just directly forward from current position).

There are X and Y camera position values that are also updated but they are being set to 0.0, 0.0. This seems like a good place to feed a position change. I have tried feeding touch position average to these values with limited success. Converting touches from screen point to world point (camera.ScreenToWorldPoint) without success. I feel I am close.

     var x:float;
     var y:float;
     
     var rotation:Quaternion;
     var position:Vector3; 
     
     var xSpeed:float;
     var ySpeed:float;
      
     var pinchSpeed:float;
     var distance:float = 15;
      
     var minimumDistance:float = 5;
     var maximumDistance:float = 100;
      
     private var lastDist:float = 0;
     private var curDist:float = 0;
     
     
     //new variables realted to attempt to move camera 
     var touchA:Touch;
     var touchB:Touch;
     
     var touchToWorldA: Vector3;
     var touchToWorldB: Vector3;
     
     var center: Vector3;
     
     
     
      
     function Update ()
     
     {        //One finger touch orbits camera
             if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
             {
                     
                     var touch = Input.touches[0];
                     x += touch.deltaPosition.x * xSpeed;
                     y -= touch.deltaPosition.y * ySpeed;
             }
             
             //Two finger pinch to zoom in/out
             if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
             {
                     
                     var touch1 = Input.touches[0];
                     var touch2 = Input.touches[1];
                     curDist = Vector2.Distance(touch1.position, touch2.position);
                     if(curDist > lastDist)
                     {
                             distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
                     }else{
                             distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
                     }
                     lastDist = curDist;      
             }
             
            
             if(distance <= minimumDistance)
             {
                     //minimum camera distance
                     distance = minimumDistance;
             }
            
             if(distance >= maximumDistance)
             {
                     //maximum camera distance
                     distance = maximumDistance;
             }
     
                
                
                //this is what I have tried so far
                if (Input.touchCount > 1 && Input.GetTouch(0).phase == TouchPhase.Began)
                { 
                 touchA = Input.touches[0];
                 touchB = Input.touches[1];
                 
                 
                 touchToWorldA = camera.ScreenToWorldPoint (Vector3 ((touchA.position.x), (touchA.position.y), camera.nearClipPlane));
                 touchToWorldB = camera.ScreenToWorldPoint (Vector3 ((touchB.position.x), (touchB.position.y), camera.nearClipPlane));
                 
                 center = (touchToWorldA + touchToWorldA / 2);
     
             }
 
            
             //Sets rotation
             rotation = Quaternion.Euler(y, x, 0);
            
            
             //Sets zoom
             
             //this was the original 
            // position = Vector3(0.0, 0.0, -distance);
             //this is the edit
             position = Vector3(center.x, center.y, -distance);
            
             //Applies rotation and position
             transform.rotation = rotation;
             transform.position = position;
             
     
     
     }
Comment
Add comment · Show 4
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 DaveA · Aug 18, 2013 at 06:23 PM 0
Share

Don't repost. If you want to bump, do an edit to your question.

avatar image pwkennedy · Aug 18, 2013 at 06:25 PM 0
Share

I attempted to edit the wording of my question only. Can you repost this?

avatar image pwkennedy · Aug 18, 2013 at 06:34 PM 0
Share

I am desperate here? $$anonymous$$y unanswered question was closed for reasons I don't understand. Can you please re-acitvate this?

avatar image DaveA · Aug 18, 2013 at 06:47 PM 0
Share

Sorry, reopened

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · Aug 18, 2013 at 06:48 PM

I think it's just a typo:

center = (touchToWorldA + touchToWorldA / 2);

should be

center = (touchToWorldA + touchToWorldB) / 2f;

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

16 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

Related Questions

Simple camera zoom function 1 Answer

Accurate Pinch Zoom 1 Answer

Hello I'm trying to write a C# about "Pinch to zoom" but it doesn't work 1 Answer

How Clamping orthographic camera between Ui Panel? 0 Answers

[UI] proper way to add pinch zoom only to one UI element on the screen? 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