Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Victor Yew · May 11, 2011 at 10:35 AM · androidcamerazoomgesturepinch

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Aug 31, 2011 at 06:37 PM 0
Share

related: http://answers.unity3d.com/questions/22500/how-to-zoom-and-pan-when-you-pinchswipe-across-scr.html

avatar image Rafes · Oct 03, 2011 at 02:09 AM 1
Share

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

avatar image CreativeStorm · Nov 02, 2011 at 11:25 PM 0
Share

Don't know if you already checked, but isn't there a Pinch'n'Zoom function in the penelope tutorial?

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AshwinSinha · Jan 24, 2013 at 07:14 AM 0
Share

Thank you so much for the code! Works like a charm!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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


camerasettings.png (53.1 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Zooming cam by finger gestures 0 Answers

How to centre a 3D pinch zoom 1 Answer

easiest way to do pinch zoom? 5 Answers

Mobile Zoom System, That Zooms In On The Right Position 1 Answer

Accurate Pinch Zoom 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges