- Home /
Pinch-zoom camera
I am trying to apply the pinch-zoom function to the main camera. BUT, all i can do is just zooming in and cannot zoom it out. AND, it seems that the line "actualDist = Mathf.Clamp(mag + actualDist, minDist, maxDist);" doesn't constrain the zooming of the camera.
Here is my code:
private var pinch:boolean; static var touch:Touch; private var touch2:Touch; private var curDist:Vector2; private var prevDist:Vector2; private var actualDist:float; private var moveFactor:float; private var mag:float; private var maxDist:float; private var minDist:float; private var zoomDist:Vector3; var target:Transform;
function Start() { resetGamePlay();
}
function resetGamePlay() {
pinch = false;
zoomDist = Vector3.zero;
minDist = 0;
maxDist = 10;
moveFactor = 0.05;
transform.LookAt(target);
}
function FixedUpdate(){
if(Input.touchCount == 2){ pinch = false; touch = Input.GetTouch(0); touch2 = Input.GetTouch(1); zoomDist = Vector3.zero;
if(touch.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved){
curDist = touch.position - touch2.position; prevDist = (touch.position - touch.deltaPosition) - (touch2.position - touch2.deltaPosition); actualDist = moveFactor (prevDist.magnitude - curDist.magnitude); mag = transform.position.magnitude; actualDist = Mathf.Clamp(mag + actualDist, minDist, maxDist); zoomDist = Vector3.forward actualDist;
pinch = true; } } }
function Update(){
if(pinch){ transform.Translate (zoomDist); transform.LookAt(target); pinch = false; } }
Appreciate if anyone can show me some clues or perhaps some examples to make it work. Thank you in advance.
Best regards, Victor Yew
I would post this to the scripting forum. Unity Answers is more for clearly asked questions with answers that are concise. This is more of a request for code help. I$$anonymous$$HO
Don't know if you already checked, but isn't there a Pinch'n'Zoom function in the penelope tutorial?
Answer by 1337GameDev · Nov 02, 2011 at 10:24 PM
This is code i created looking around on forums and researching. Hope it helps.
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);
}
}
}
}
Here is code that i made that should; work nicely. Just make sure that you add your camera to the inspector for the public variable "selectedCamera". Enjoy!
Answer by JP_Ferreira · Sep 18, 2014 at 02:48 PM
Unity have a tut on how to do that!
http://unity3d.com/learn/tutorials/modules/beginner/platform-specific/pinch-zoom
This work aswell but, they have a "cleaner" solution.
Answer by belobig · Jun 03, 2015 at 06:56 AM
Here's what I came up with, after combining what I found from a few scripts online, and my own fixes. Of course, you may need to tweak this to your preferences or to work with your particular game. I hope it helps!
using UnityEngine;
using System.Collections;
public class TouchCameraControl : MonoBehaviour
{
public float moveSensitivityX = 0.5f;
public float moveSensitivityZ = 0.5f;
public bool updateZoomSensitivity = true;
public float orthoZoomSpeed = 0.05f;
public float minZoom = 1.0f;
public float maxZoom = 20.0f; //best set at 20
public bool invertMoveX = false;
public bool invertMoveZ = false;
public float mapWidth = 85.0f;
public float mapLength = 60.0f;
public float inertiaDuration = 1.0f;
private Camera _camera;
private float minX, maxX, minY, maxY;
private float horizontalExtent, verticalExtent;
private float scrollVelocity = 0.0f;
private float timeTouchPhaseEnded;
private Vector3 scrollDirection = Vector3.zero;
void Start ()
{
_camera = Camera.main;
//maxZoom = 0.5f * (mapWidth / _camera.aspect); //use this if you need max zoom more than 20
//if (mapWidth > mapLength) //use this if you need max zoom more than 20
// maxZoom = 0.5f * (mapLength/ _camera.aspect); //use this if you need max zoom more than 20
if (_camera.orthographicSize > maxZoom)
_camera.orthographicSize = maxZoom;
CalculateLevelBounds ();
}
void Update ()
{
if (updateZoomSensitivity)
{
moveSensitivityX = _camera.orthographicSize / 5.0f;
moveSensitivityZ = _camera.orthographicSize / 5.0f;
}
Touch[] touches = Input.touches;
if (touches.Length < 1)
{
//if the camera is currently scrolling
if (scrollVelocity != 0.0f)
{
//slow down over time
float t = (Time.time - timeTouchPhaseEnded) / inertiaDuration;
float frameVelocity = Mathf.Lerp (scrollVelocity, 0.0f, t);
_camera.transform.position += -(Vector3)scrollDirection.normalized * (frameVelocity * 0.05f) * Time.deltaTime;
if (t >= 1.0f)
scrollVelocity = 0.0f;
}
}
if (touches.Length > 0)
{
//Single touch (move)
if (touches.Length == 1)
{
if (touches[0].phase == TouchPhase.Began)
{
scrollVelocity = 0.0f;
}
else if (touches[0].phase == TouchPhase.Moved)
{
Vector3 delta = touches[0].deltaPosition;
float positionX = delta.x * moveSensitivityX * Time.deltaTime;
positionX = invertMoveX ? positionX : positionX * -1;
float positionZ = delta.z * moveSensitivityZ * Time.deltaTime;
positionZ = invertMoveZ ? positionZ : positionZ * -1;
_camera.transform.position += new Vector3 (positionX, 0, positionZ);
scrollDirection = touches[0].deltaPosition.normalized;
scrollVelocity = touches[0].deltaPosition.magnitude / touches[0].deltaTime;
if (scrollVelocity <= 100)
scrollVelocity = 0;
}
else if (touches[0].phase == TouchPhase.Ended)
{
timeTouchPhaseEnded = Time.time;
}
}
//Double touch (zoom)
if (touches.Length == 2)
{
Vector3 cameraViewsize = new Vector3 (_camera.pixelWidth, _camera.pixelHeight);
Touch touchOne = touches[0];
Touch touchTwo = touches[1];
Vector3 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
Vector3 touchTwoPrevPos = touchTwo.position - touchTwo.deltaPosition;
float prevTouchDeltaMag = (touchOnePrevPos - touchTwoPrevPos).magnitude;
float touchDeltaMag = (touchOne.position - touchTwo.position).magnitude;
float deltaMagDiff = prevTouchDeltaMag - touchDeltaMag;
_camera.transform.position += _camera.transform.TransformDirection ((touchOnePrevPos + touchTwoPrevPos - cameraViewsize) * _camera.orthographicSize / cameraViewsize.y);
_camera.orthographicSize += deltaMagDiff * orthoZoomSpeed;
_camera.orthographicSize = Mathf.Clamp (_camera.orthographicSize, minZoom, maxZoom) - 0.001f;
_camera.transform.position -= _camera.transform.TransformDirection (((Vector3)touchOne.position + (Vector3)touchTwo.position - cameraViewsize) * _camera.orthographicSize / cameraViewsize.y);
CalculateLevelBounds ();
}
}
}
void CalculateLevelBounds ()
{
verticalExtent = _camera.orthographicSize;
horizontalExtent = _camera.orthographicSize * Screen.width / Screen.height;
minX = horizontalExtent - mapWidth / 2.0f;
maxX = mapWidth / 2.0f - horizontalExtent;
minY = verticalExtent - mapLength / 2.0f;
maxY = mapLength / 2.0f - verticalExtent;
}
void LateUpdate ()
{
Vector3 limitedCameraPosition = _camera.transform.position;
limitedCameraPosition.x = Mathf.Clamp (limitedCameraPosition.x, minX, maxX);
limitedCameraPosition.y = Mathf.Clamp (limitedCameraPosition.y, minY, maxY);
_camera.transform.position = limitedCameraPosition;
}
void OnDrawGizmos ()
{
Gizmos.DrawWireCube (Vector3.zero, new Vector3 (mapWidth, 0, mapLength));
}
}
[1]: /storage/temp/47539-camerasettings.png
Your answer
Follow this Question
Related Questions
How to centre a 3D pinch zoom 1 Answer
Accurate Pinch Zoom 1 Answer
easiest way to do pinch zoom? 5 Answers
Zooming cam by finger gestures 0 Answers
Mobile Zoom System, That Zooms In On The Right Position 1 Answer