- Home /
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,
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.
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;
}
$$anonymous$$ay not be the exact answer I'm looking for, but thanks for sharing.
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?
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
Follow this Question
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