• 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
0
Question by The_Icaruz · Feb 13, 2019 at 09:49 PM · touchzoommobile devicesorthographic camerapanning

Touch code for panning and zooming not working

I'm using Unity 2017 and working in 2D with an orthographic camera.

The problem is I can't wrap my head arround whats wrong with my code.


I just want to pan and zoom the camera with touch and only do this when not over an UI element but nothing is working.


If you could help me or point me out where I made a mistake, I would be so thankful.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class CameraController : MonoBehaviour {
     
     Vector2 StartPosition;
     Vector2 DragStartPosition;
     Vector2 DragNewPosition;
     Vector2 Finger0Position;
     float DistanceBetweenFingers;
 
     public float zoomOutMin = 6;
     public float zoomOutMax = 20;
     
     void Update () {
         checkTouchOnScreen ();
     }
 
     void checkTouchOnScreen () {
 
         if (Input.touchCount > 0 && Input.GetTouch(0).phase ==TouchPhase.Began) {
             //Make sure finger is NOT over a UI element
             if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
                 OnScreenTouched ();
             }
         }
     }
 
     void OnScreenTouched () {
         Debug.Log ("not over UI");
 
         if (Input.touchCount == 1) {
             if (Input.GetTouch(0).phase == TouchPhase.Moved) {
                 Vector2 NewPosition = GetWorldPosition();
                 Vector2 PositionDifference = NewPosition - StartPosition;
                 Camera.main.transform.Translate (-PositionDifference);
             }
             StartPosition = GetWorldPosition ();
         } else if (Input.touchCount == 2) {
             if (Input.GetTouch(1).phase == TouchPhase.Moved) {
 
                 DragNewPosition = GetWorldPositionOfFinger(1);
                 Vector2 PositionDifference = DragNewPosition - DragStartPosition;
 
                 if (Vector2.Distance (DragNewPosition, Finger0Position) < DistanceBetweenFingers) {
                     Camera.main.orthographicSize += (PositionDifference.magnitude);
                 }
 
                 if (Vector2.Distance (DragNewPosition, Finger0Position) >= DistanceBetweenFingers) {
                     Camera.main.orthographicSize -= (PositionDifference.magnitude);
                 }
                 DistanceBetweenFingers = Vector2.Distance (DragNewPosition, Finger0Position);
             }
             DragStartPosition = GetWorldPositionOfFinger (1);
             Finger0Position = GetWorldPositionOfFinger (0);
         }
     }
 
     Vector2 GetWorldPosition () {
         return Camera.main.ScreenToWorldPoint (Input.mousePosition);
     }
 
     Vector2 GetWorldPositionOfFinger (int FingerIndex) {
         return Camera.main.ScreenToWorldPoint (Input.GetTouch (FingerIndex).position);
     }
 }
 



Comment
Add comment
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

1 Reply

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

Answer by Leycarno · Feb 14, 2019 at 07:35 AM

Your code works just for the moment the touch starts. So you get the first position but no changes.

      void checkTouchOnScreen () {
  
          if (Input.touchCount > 0 && Input.GetTouch(0).phase ==TouchPhase.Began) {

Handle the "TouchPhase.Began" together with the other ones:

https://docs.unity3d.com/ScriptReference/Touch-phase.html

Comment
Add comment · Show 13 · 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 The_Icaruz · Feb 14, 2019 at 08:07 AM 0
Share

Thank you for your answer.


So I've read the docs but, I'm not sure if I understand you right with "Handle the "TouchPhase.Began" together with the other ones".


How would I do this exactly with my current code?

Should I use a switch statement?


EDIT :

At the moment I can't try it, but do you mean something like this :

avatar image The_Icaruz · Feb 14, 2019 at 08:39 AM 0
Share

Can't post this code to my other reply, sorry.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.EventSystems;
  
  public class CameraController : $$anonymous$$onoBehaviour {
      
      Vector2 StartPosition;
      Vector2 DragStartPosition;
      Vector2 DragNewPosition;
      Vector2 Finger0Position;
      float DistanceBetweenFingers;
      bool isZooming;
  
      public float zoomOut$$anonymous$$in = 6;
      public float zoomOut$$anonymous$$ax = 20;
      
      void Update () {
          checkTouchOnScreen ();
     }
  
     void checkTouchOnScreen () {
  
         if (Input.touchCount > 0) {
              //$$anonymous$$ake sure finger is NOT over a UI element
             if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
                 OnScreenTouched ();
             }
         }
     }
  
     void OnScreenTouched () {
          Debug.Log ("not over UI");
  
         if (Input.touchCount == 1 && !isZooming) {
         Touch touch = Input.GetTouch (0);
             switch (touch.phase) {
                 case TouchPhase.Began:
                     StartPosition = GetWorldPosition ();
                 break;
                 
                 case TouchPhase.$$anonymous$$oved:
                 Vector2 NewPosition = GetWorldPosition(); // EDIT "NewPosition"
                 Vector2 PositionDifference = NewPosition - StartPosition;
                 Camera.main.transform.Translate (-PositionDifference);
                 break;
                 
                 case TouchPhase.Ended:
                 break;
              }
          } else if (Input.touchCount == 2) {
          Touch touch = Input.GetTouch (1);
              isZooming = true;
              switch (touch.phase) {
                 case TouchPhase.Began:
                     DragStartPosition = GetWorldPositionOfFinger (1);
                     Finger0Position = GetWorldPositionOfFinger (0);
                 break;
                 
                 case TouchPhase.$$anonymous$$oved:
                     DragNewPosition = GetWorldPositionOfFinger(1);
                     Vector2 PositionDifference = DragNewPosition - DragStartPosition;
  
                     if (Vector2.Distance (DragNewPosition, Finger0Position) < DistanceBetweenFingers) {
                     Camera.main.orthographicSize += (PositionDifference.magnitude);
                     }
  
                     if (Vector2.Distance (DragNewPosition, Finger0Position) >= DistanceBetweenFingers) {
                     Camera.main.orthographicSize -= (PositionDifference.magnitude);
                     }
                     DistanceBetweenFingers = Vector2.Distance (DragNewPosition, Finger0Position);
                 break;
                 
                 case TouchPhase.Ended:
                     isZooming = false;
                 break;
             }
         }
     }
  
     Vector2 GetWorldPosition () {
         return Camera.main.ScreenToWorldPoint (Input.mousePosition);
     }
  
     Vector2 GetWorldPositionOfFinger (int FingerIndex) {
         return Camera.main.ScreenToWorldPoint (Input.GetTouch (FingerIndex).position);
     }
 }

avatar image Leycarno The_Icaruz · Feb 14, 2019 at 11:35 AM 1
Share

That's what I meant. Sorry for the delay - I can look deeper into the code after work...

avatar image The_Icaruz Leycarno · Feb 14, 2019 at 12:28 PM 0
Share

No problem at all, glad you are helping me with my problem.

avatar image Leycarno The_Icaruz · Feb 14, 2019 at 11:41 AM 1
Share

is this the complete code? Where did "NewPosition" come from?

avatar image The_Icaruz Leycarno · Feb 14, 2019 at 12:19 PM 0
Share

Oh maybe i deleted it. Sorry.

In the original question i get the "NewPosition".

And no it is not the complete code just the snippets for the panning amd zooming. The other code is for getting the target when it moves.


EDIT : added the snippet fot the "NewPosition" in the reply

avatar image Leycarno The_Icaruz · Feb 14, 2019 at 11:59 AM 1
Share

So it's untested, but maybe it helps:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class CameraController : $$anonymous$$onoBehaviour {
     Vector2 _lastPosition;
     Vector2 _dragStartPosition;
     Vector2 _dragNewPosition;
     Vector2 _finger0Position;
     float _distanceBetweenFingers;
 
     public float _zoomOut$$anonymous$$in = 6;
     public float _zoomOut$$anonymous$$ax = 20;
 
     Camera _mainCam;
 
     void Start() {
         _mainCam = Camera.main;
     }
 
 
     void Update() {
         CheckTouchOnScreen();
     }
 
     void CheckTouchOnScreen() {
         if (Input.touchCount <= 0) return;
 
         //$$anonymous$$ake sure finger is NOT over a UI element
         if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return;
         Debug.Log("not over UI");
 
         switch (Input.touchCount) {
             case 1:
                 PanCam();
                 break;
             case 2:
                 ZoomCam();
                 break;
             
             // TODO: what about > 2 touches?
             
         }
     }
 
     void PanCam() {
         if (Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
             var newPosition = GetWorldPosition();
             var positionDifference = newPosition - _lastPosition;
             _mainCam.transform.Translate(-positionDifference);
         }
 
         _lastPosition = GetWorldPosition();
     }
 
     void ZoomCam() {
         _dragNewPosition = GetWorldPositionOfFinger(1);
         
         if (Input.GetTouch(1).phase == TouchPhase.$$anonymous$$oved) {
             var positionDifference = _dragNewPosition - _dragStartPosition;
 
             if (Vector2.Distance(_dragNewPosition, _finger0Position) < _distanceBetweenFingers) {
                 _mainCam.orthographicSize += (positionDifference.magnitude);
             }
 
             if (Vector2.Distance(_dragNewPosition, _finger0Position) >= _distanceBetweenFingers) {
                 _mainCam.orthographicSize -= (positionDifference.magnitude);
             }
 
             _distanceBetweenFingers = Vector2.Distance(_dragNewPosition, _finger0Position);
         }
 
         _dragStartPosition = _dragNewPosition;
         _finger0Position = GetWorldPositionOfFinger(0);
     }
 
     Vector2 GetWorldPosition() {
         return _mainCam.ScreenToWorldPoint(Input.mousePosition);
     }
 
     Vector2 GetWorldPositionOfFinger(int fingerIndex) {
         return _mainCam.ScreenToWorldPoint(Input.GetTouch(fingerIndex).position);
     }
 }
avatar image The_Icaruz Leycarno · Feb 14, 2019 at 12:24 PM 0
Share

Thank you so much, I will try it when I'm at home. But looks promising to me.

But why return if touchCount > 0 ?

 void CheckTouchOnScreen() { 
 if (Input.touchCount > 0) return;



When I'm at home I'll let you know how it works.

Show more comments
avatar image The_Icaruz · Feb 15, 2019 at 01:47 PM 0
Share

So i thought about the zooming function and wrote my own script for it now it all works like a charm, thank you so much for helping me. ;D

avatar image Leycarno The_Icaruz · Feb 15, 2019 at 02:09 PM 1
Share

Always a pleasure :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

133 People are following this question.

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

Related Questions

How do you differentiate between pinch to zoom with two fingers and a two finger drag? 2 Answers

Orbit Camera Zoom limit 1 Answer

How to add Pinch to Zoom? 0 Answers

How to scale objects individual with pinch zoom in ARCore and set a Min/Max scale? 1 Answer

Magnifying Glass Unity iOS 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges