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 Solitude01 · Oct 13, 2011 at 02:35 AM · scrollviewpanning

2 finger scrolling

I couldn't find it anywhere, so here I ask:

What's the concept for 2-finger scrolling? I made my check like this:

function Update() { if (Input.touchCount == 2) { var dist : float = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position); var deltaFingerDistance : float;

   if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
   {
      lastFingerDistance = dist;
      deltaFingerDistance = 0;
   }
   else
   {
      deltaFingerDistance = dist-lastFingerDistance;
   }
   lastFingerDistance = dist

   if (Mathf.Abs(deltaFingerDistance)>0.5)
   {
      //pinch zoom code here
   }
   else
   {
      if (dist<10)
      {
         if (Input.GetTouch(0).phase == TouchPhase.Began)
         {
             initFingerPos = Input.GetTouch(0).position;
             cameraInitPos = theCamera.transform.position;
         }
         theCamera.transform.position = cameraInitPos + (Input.GetTouch(0).position-initFingerPos);
      }
   }

} }

The panning right now seems jumping over here and there, so I guess there's something wrong with the camera panning code, but it's hard to figure out where it went wrong.

Thanks in Advance,

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 Solitude01 · Oct 13, 2011 at 04:42 AM 0
Share

I've changed the code a bit

function Update () { if (Input.touchCount == 2) { var dist: float = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position); var deltaFingerDistance : float;

     if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
     {
         lastFingerDistance = dist;
         deltaFingerDistance = 0;
         print("first touch!");
     }
     else
     {
         deltaFingerDistance = dist-lastFingerDistance;
     }
     lastFingerDistance = dist;
     
     print("finger id : " + Input.GetTouch(0).fingerId + ", " + Input.GetTouch(1).fingerId);
     
     if (Input.GetTouch(0).fingerId == 0 && Input.GetTouch(1).fingerId == 1)
     {
         if ($$anonymous$$athf.Abs(deltaFingerDistance)>20)
         {
             if (deltaFingerDistance < 0)
             {
                 theCamera.fieldOfView = $$anonymous$$athf.Clamp(theCamera.fieldOfView-deltaFingerDistance*0.1, 15, 80);
             }
             else
             {
                 print("finger distance : " + dist);
                 if (dist< 200)
                 {
                     //The 2 fingers are close enough -> do panning
                     theCamera.transform.position += Input.GetTouch(0).deltaPosition*0.1;
                 }
             }
         }
     }
 }
 else if (Input.touchCount > 2)
 {
     //This case need to be handled like this, so even though there's no code here. Just make this place holder
 }

} 20 is the threshold of the speed. 200 is the threshold how far the fingers are apart that the system would consider the user is trying to do camera panning.

Seems to be working fine for now except the occasional zoo$$anonymous$$g while panning. I think giving another threshold level is necessary.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by jahroy · Oct 13, 2011 at 04:18 AM

For me, that seems like a confusing way to deal with touch input. That being said, I'm a buffoon.

Below is how we do pan/zoom for touchscreen.

The following script started as the camera orbit script that comes in the Standard Assets folder.

We use three fingers for pan and two fingers for zoom, but you get the idea...

var panTranslate : Vector3 = Vector3.zero; var distance : float = 5.0;

/ vars used to track camera rotation /

var rotation : Quaternion; var position : Vector3; var x : float; var y : float;

function LateUpdate () { / zoom in/out for two finger pinch /

 if ( Input.touchCount == 2 ) {

     var fingerOne    =  Input.GetTouch(0);
     var fingerTwo    =  Input.GetTouch(1);
     var currentDist  =  fingerOne.position - fingerTwo.position;

     var deltaOne     =  fingerOne.position - fingerOne.deltaPosition;
     var deltaTwo     =  fingerTwo.position - fingerTwo.deltaPosition;
     var lastDist     =  deltaOne - deltaTwo;

     var deltaDist    =  currentDist.magnitude - lastDist.magnitude;
     var theDelta     =  deltaDist * Time.deltaTime;

     distance        -=  theDelta * zoomSpeed * Mathf.Abs(distance); 

     /* clamp the distance so you can't get too close or far */

     distance = clampDistance (distance, distanceMinLimit, distanceMaxLimit);
 }

 /* pan for three touches */

 if ( Input.touchCount == 3 ) {

     var totalMove = Vector2.zero;

     for ( var thisTouch : Touch in Input.touches ) {
         totalMove += thisTouch.deltaPosition;
     }

     /* calculate average movement for each touch */

     var panMove    =  totalMove / Input.touchCount;

     var deltaPanY  =  panMove.y * transform.up    * distance * speedFactor;
     var deltaPanX  =  panMove.x * transform.right * distance * speedFactor;

     panTranslate  -=  deltaPanY;
     panTranslate  -=  deltaPanX;

     /* clamp the pan vector to constrain movement */

     panTranslate = clampPanTranslate(panTranslate);
 }

 /* ignore rotation for this example... */

 rotation = Quaternion.Euler(y, x, 0);
 position = rotation * Vector3(0.0, 0.0, -distance) + target.position + panTranslate;

 transform.rotation = rotation;
 transform.position = position;

}

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 Solitude01 · Oct 13, 2011 at 04:35 AM 0
Share

$$anonymous$$ay not be the exact answer I'm looking for, but thanks for sharing.

avatar image StoneFish · Sep 02, 2012 at 02:18 PM 0
Share

Hi - i receive a lot of errors on the above jahroy for missing variable declarations... i tried to figure most being floats, but some aren't. Can you post those?

avatar image jahroy · Sep 02, 2012 at 10:01 PM 0
Share

Deter$$anonymous$$ing the variable types will be a great way to help you understand the code if you plan to use it.

They should be easy to figure out by looking at what's being assigned.

Examples:

  • fingerOne must be a Touch: it's set to the return value of Input.getTouch()

  • currentDist must be a Vector2: it's set to Touch.position $$anonymous$$us Touch.deltaPosition (both are Vector2s)

All of this stuff is easily found by reading the script reference.

If the types aren't already obvious to you, you'll be well served to figure them out!

All you have to do to read the documentation for Input.getTouch() is google `unity input.gettouch` and click the first link (the script reference). This is how you learn to script in Unity :)

(the script reference is built into the help menu and is searchable, too)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I set the mousewheel scroll speed in a ScrollView? 3 Answers

Scroll Bar 2 Answers

How can I replace the behaviour of a GUILayout.scrollView on MouseWheel event? 2 Answers

End scrollview c# 1 Answer

Can you have a scrollview inside a textfield? 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