Pinch zoom: from static to movable

Hi, all,

I am using the following pinch-to-zoom script on a camera. Can someone suggest a way to constrain its movement? Also, I am trying to figure out how I can keep it static when zoomed out and movable within defined constraints when zoomed in.

Thanks,

Greg

using UnityEngine;
using System.Collections;

public class Pinch : MonoBehaviour {




   // Use this for initialization
   void Start () {
   
   }
   
   // Update is called once per frame
   void Update () {
     
iPhoneTouch touch = iPhoneInput.GetTouch(0);
iPhoneTouch touch2 = iPhoneInput.GetTouch(1);

// Find out how the touches have moved relative to eachother:
Vector2 curDist = touch.position - touch2.position;
Vector2 prevDist = (touch.position - touch.positionDelta) - (touch2.position - touch2.positionDelta);

float delta = curDist.magnitude - prevDist.magnitude;

Debug.Log(delta);

Camera.main.transform.Translate(0, 0, delta);

   
   }
}

[/i]

Decide on a maximum and minimum distance for the camera’s Z coordinate. Then, instead of passing your delta value to transform.Translate, add it to a distance variable and use Mathf.Clamp to constrain its value:-

var minDist: float;
var maxDist: float;
var actualDist: float;

   ...
actualDist = Mathf.Clamp(actualDist + delta, minDist, maxDist);
transform.position = Vector3.forward * distance;

You will need a function something like the following to determine the size of the camera’s view rectangle at a given distance:-

function CamViewRectAtDistance(cam: Camera, dist: float) {
	var halfFOVRadians: float = Mathf.Deg2Rad * cam.fieldOfView * 0.5;
	var y: float = Mathf.Tan(halfFOVRadians) * dist * 2.0;
	var x: float = y * cam.aspect;
	return new Vector2(x, y);
}

Basically, the view rectangle will get smaller the closer the camera is to the subject. The view is a kind of mini-rectangle within the total viewable area - the constraint is that the view should stop moving if the mini-rectangle goes outside the viewable area.

Hi, Andeee,

I forgot to reply back and thank you for your help on this one. Thanks so much for you time and suggestions!

Best,

Greg

Hi,

would be great if you posted the finished script! :slight_smile:

Hey Andee, how does this actually work? In the first snippet you pluck the variable “distance” out of nowhere. When I try and implement it I get horrid snapping between min and max distance.

And so in the case of this function, how would it be called? Obviously not from update, but when theres two fingers touching or …?

Thanks
AaronC

Hey Andee, I am stucked here as well. I have no idea where the “distance” variable comes from. I’ve got the feeling I am almost there. I am just confused with the “actualDist” and the “distance” variables from your example above. Please help.

T.Q.
victor

Hi,

“distance” should be replaced by actualDist, just calculated the previous line.

Thanks for the reply.

I’ve actually try to substitute the distance variable with actualDist, the result is still not according to what is expected.
Whenever I move the fingers across the screen, the camera begin to zoom from a certain point and not from its original z-coordinate.

This is my code:

var touch:Touch;
var touch2:Touch;
var curDist:Vector2;
var prevDist:Vector2;
var delta:float;
var actualDist:float;
var maxDist:float;
var minDist:float;
var target:Transform;


function Update(){

transform.LookAt(target);

if(Input.touchCount == 2){
touch = Input.GetTouch(0);
touch2 = Input.GetTouch(1);

if(touch.phase == TouchPhase.Moved  touch2.phase == TouchPhase.Moved){
// Find out how the touches have moved relative to eachother:
curDist = touch.position - touch2.position;
prevDist = (touch.position - touch.deltaPosition) - (touch2.position - touch2.deltaPosition);
delta = curDist.magnitude - prevDist.magnitude;
actualDist = Mathf.Clamp(actualDist + delta, minDist, maxDist);
transform.position = Vector3.forward * actualDist;

}
}
}

@VictorYew : maybe you should clamp the transform.position instead of the distance ?

so, you mean it changes to this:

actualDist = actualDist + delta;
transform.position = Vector3(0,0,Mathf.Clamp(actualDist, minDist, MaxDist));

I tried this but it still doesn’t work out. Same problem persists.

Thanks for the reply anyway. Ciaravoh.

Guys,

I found a very good resource : http://wh1t3s.com/tag/3d/

it’s working fine including the clamp.

Thanks for the sharing. I’ve read about it and it’s very detailed. T.Q.

Thanks for share!