- Home /
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;
}
Don't repost. If you want to bump, do an edit to your question.
I attempted to edit the wording of my question only. Can you repost this?
I am desperate here? $$anonymous$$y unanswered question was closed for reasons I don't understand. Can you please re-acitvate this?
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;