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);
}
}
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.
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 …?
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.
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;
}
}
}