- Home /
easiest way to do pinch zoom?
does somebody have example of how to do pinch zoom like in iphone? i am developing for android and currently i have zoom in/out with two fingers up/down and i would like to change that.
thank you!
Answer by mcarriere · Sep 08, 2011 at 03:14 PM
Check the distance between two touches, and move your camera accordingly.
if (Input.touchCount >= 2)
{
Vector2 touch0, touch1;
float distance;
touch0 = Input.GetTouch(0).position;
touch1 = Input.GetTouch(1).position;
distance = Vector2.Distance(touch0, touch1);
}
From here you could clamp your distance to a min/max distance, and in turn, use that to change the position or FOV of your camera.
If you do a direct mapping, it means that you're going to have two fingers close together always mean "zoomed in", and vice versa for two fingers far apart.
You could also save the last distance in a class variable, and just do something on the delta between the last saved distance, and the current distance change.
Answer by 1337GameDev · Nov 02, 2011 at 01:33 AM
You are missing key points. Here is a step by step on how to do it: 1: if statement to check # if fingers touching and if they are in the moved phase 2: within the if, figure out the distance between the two points current position 3: within the if calculate the previous position by subtracting the current positions with the positionDelta (the distance each touch moved) 4: then within the if, declare a new if based on weather positions of each touch moved farther apart by subtracting the distance each touch moved from previous to current update (using position and previous position.positionDelta respectively). Separate into two if statements where one checks if one distance is larger and one is smaller to see if it was an inward or outward pinch gesture. 5. Apply your actions under each if statement respectively and add mathf.clamp(input float, min float, max float) (float is data type) to limit the action bounds or a speed mod here if wanted.
using UnityEngine; using System.Collections;
public class CameraZoomPinch : MonoBehaviour { public int speed = 4; public Camera selectedCamera; public float MINSCALE = 2.0F; public float MAXSCALE = 5.0F; public float minPinchSpeed = 5.0F; public float varianceInDistances = 5.0F; private float touchDelta = 0.0F; private Vector2 prevDist = new Vector2(0,0); private Vector2 curDist = new Vector2(0,0); private float speedTouch0 = 0.0F; private float speedTouch1 = 0.0F;
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () {
if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
curDist = Input.GetTouch(0).position - Input.GetTouch(1).position; //current distance between finger touches
prevDist = ((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); //difference in previous locations using delta positions
touchDelta = curDist.magnitude - prevDist.magnitude;
speedTouch0 = Input.GetTouch(0).deltaPosition.magnitude / Input.GetTouch(0).deltaTime;
speedTouch1 = Input.GetTouch(1).deltaPosition.magnitude / Input.GetTouch(1).deltaTime;
if ((touchDelta + varianceInDistances <= 1) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,90);
}
if ((touchDelta +varianceInDistances > 1) && (speedTouch0 > minPinchSpeed) && (speedTouch1 > minPinchSpeed))
{
selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
}
}
}
}
hey this script didn't worked for me as well.. :( Have attached this script on main camera (orthographic).can you tell me whats the issue ?
You have to assign the selectedCamera variable in the inspector, or find the camera in start and assign it via script.
Answer by timebenter · Mar 17, 2021 at 10:54 PM
Here is some nice and simple code. The variable touchDist is the change in distance between your two fingers.
float touchDist = 0;
float lastDist = 0;
void Update()
{
if (Input.touchCount == 2)
{
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
if (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Began)
{
lastDist = Vector2.Distance(touch1.position, touch2.position);
}
if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved)
{
float newDist = Vector2.Distance(touch1.position, touch2.position);
touchDist = lastDist - newDist;
lastDist = newDist;
// Your Code Here
Camera.main.fieldOfView += touchDist * 0.1f;
}
}
}
Answer by Helix_abdu · May 08, 2015 at 10:26 PM
this is a good tutorial from unity-tutorials for beginners https://unity3d.com/learn/tutorials/modules/beginner/platform-specific/pinch-zoom
Answer by AngryGamemaster · Dec 06, 2019 at 10:17 PM
im using a camera set to orthographic in a 3d space. so i can go between 3d isometric and straight on like a platformer. so ill include my code i use. after seeing that touch information.
using UnityEngine;
namespace Version3
{
public class PinchZoom : MonoBehaviour
{
// orthographic sizes
public static float MaxZoom = 6f;
public static float StdZoom = 1.44f;
public static float MinZoom = 0.1f;
public static PinchZoom HANDLE;
public Camera targetCamera;
public float initialOrthoSize;
void Awake(){
HANDLE = this;
initialOrthoSize = targetCamera.orthographicSize;
}
public void ChangeCamera(Camera newTargetCamera){
float newInitialSize = newTargetCamera.orthographicSize;
newTargetCamera.orthographicSize = targetCamera.orthographicSize;
targetCamera.orthographicSize = initialOrthoSize;
initialOrthoSize = newInitialSize;
}
public float panSpeedAdjustment = 1.0f;
public float currentOrthoSize = 0f;
public float lastDistance = 0f;
void Update()
{
if (Input.touchCount >= 2)
{
//twoTouches = true;
Vector2 touch0, touch1;
float distance;
touch0 = Input.GetTouch (0).position;
touch1 = Input.GetTouch (1).position;
distance = Vector2.Distance (touch0, touch1);
if (lastDistance == 0f)
{
lastDistance = distance;
currentOrthoSize = targetCamera.orthographicSize;
}
else
{
if (distance > lastDistance)
{
// zoom in
float multiplier = lastDistance/distance;
// 1/1.2 = .833
float newOrthoSize = currentOrthoSize * multiplier;
if (newOrthoSize >= MinZoom)
{
targetCamera.orthographicSize = newOrthoSize;
AdjustPanSpeed (newOrthoSize);
}
else
{
targetCamera.orthographicSize = MinZoom;
AdjustPanSpeed (newOrthoSize);
}
}
else
{
// zoom out max 6 float
float multiplier = lastDistance / distance;
// 1.2/1 = 1.2
float newOrthoSize = currentOrthoSize * multiplier;
if (newOrthoSize <= MaxZoom)
{
targetCamera.orthographicSize = newOrthoSize;
AdjustPanSpeed (newOrthoSize);
}
else
{
targetCamera.orthographicSize = MaxZoom;
AdjustPanSpeed (newOrthoSize);
}
}
}
} else {
lastDistance = 0f;
currentOrthoSize = 0f;
AdjustPanSpeed (targetCamera.orthographicSize);
}
}
// Exists for other zoom changing functions in other places.
public void AdjustPanSpeed(float orthographicSize){
if (orthographicSize <= StdZoom) {
panSpeedAdjustment = orthographicSize/StdZoom; // slows down panning
} else {
panSpeedAdjustment = 1.0f; // no speed change
}
}
}
}