• 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 hashtagz28 · Dec 11, 2018 at 12:10 PM · camerazoompanning

panning & zooming using multitouch

hi, I am the problem i am running into is that i need to pan the camera and zoom in and other using two touch's as my input the problem is when i move my fingers to pan it also results in zooming in i have tried using a dot product of the two touch's but cant come to a solution.Also i am using one into to rotate the object on the screen thank u for the answer in advance.

Comment
Add comment · Show 1
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 hashtagz28 · Dec 11, 2018 at 12:11 PM 0
Share

I forgot to add I was trying a package to get what i wanted

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zohaibzaidi · Dec 11, 2018 at 02:25 PM

This is a script I used a couple of times.. Should fix your problem.. You can remove the HasReachedBounds method. The code might need some modification. Usually works straightforward for me.

 // Just add this script to your camera. It doesn't need any configuration.
 
 using UnityEngine;
 
 public class TouchCamera : MonoBehaviour {
     Vector2?[] oldTouchPositions = {
         null,
         null
     };
     Vector2 oldTouchVector;
     float oldTouchDistance;
 
     private Camera camera;
 
     public SpriteRenderer mapSprite;
 
     Vector3 spriteMin;
     Vector3 spriteMax;
 
     private void Start()
     {
         camera = GetComponent<Camera>();
 
         Bounds spriteBounds = mapSprite.bounds;
         spriteMin = spriteBounds.min;
         spriteMax = spriteBounds.max;
     }
 
     enum BoundReached
     {
         left,
         right,
         top,
         bottom
     }
 
     BoundReached boundReached = BoundReached.left;
 
 
     void Update()
     {
 
         if (HasReachedBounds())
         {
             //lerp back
             if (Input.touchCount == 0)
             {
                 var vertExtent = camera.orthographicSize;
                 var horzExtent = vertExtent * Screen.width / Screen.height;
 
                 if (boundReached == BoundReached.left)
                 {
                     transform.position = Vector3.Lerp(transform.position, new Vector3(spriteMin.x + horzExtent + 1, transform.position.y, transform.position.z), Time.deltaTime * 5);
                 }
                 else if (boundReached == BoundReached.right)
                 {
                     transform.position = Vector3.Lerp(transform.position, new Vector3(spriteMax.x - horzExtent - 1, transform.position.y, transform.position.z), Time.deltaTime * 5);
                 }
                 else if (boundReached == BoundReached.top)
                 {
                     transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, spriteMax.y - vertExtent - 1, transform.position.z), Time.deltaTime * 5);
                 }
                 else if (boundReached == BoundReached.bottom)
                 {
                     transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, spriteMin.y + vertExtent + 1, transform.position.z), Time.deltaTime * 5);
                 }
             }
             
         }
         else
         {
             if (Input.touchCount == 0)
             {
                 oldTouchPositions[0] = null;
                 oldTouchPositions[1] = null;
             }
             else if (Input.touchCount == 1)
             {
                 if (oldTouchPositions[0] == null || oldTouchPositions[1] != null)
                 {
                     oldTouchPositions[0] = Input.GetTouch(0).position;
                     oldTouchPositions[1] = null;
                 }
                 else
                 {
                     Vector2 newTouchPosition = Input.GetTouch(0).position;
 
                     transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] - newTouchPosition) * camera.orthographicSize / camera.pixelHeight * 2f));
 
                     oldTouchPositions[0] = newTouchPosition;
                 }
             }
             else
             {
                 if (oldTouchPositions[1] == null)
                 {
                     oldTouchPositions[0] = Input.GetTouch(0).position;
                     oldTouchPositions[1] = Input.GetTouch(1).position;
                     oldTouchVector = (Vector2)(oldTouchPositions[0] - oldTouchPositions[1]);
                     oldTouchDistance = oldTouchVector.magnitude;
                 }
                 else
                 {
                     Vector2 screen = new Vector2(camera.pixelWidth, camera.pixelHeight);
 
                     Vector2[] newTouchPositions = {
                     Input.GetTouch(0).position,
                     Input.GetTouch(1).position
                 };
                     Vector2 newTouchVector = newTouchPositions[0] - newTouchPositions[1];
                     float newTouchDistance = newTouchVector.magnitude;
 
                     transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * camera.orthographicSize / screen.y));
                     //transform.localRotation *= Quaternion.Euler(new Vector3(0, 0, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f));
                     camera.orthographicSize *= oldTouchDistance / newTouchDistance;
                     camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, 5, 40);
                     transform.position -= transform.TransformDirection((newTouchPositions[0] + newTouchPositions[1] - screen) * camera.orthographicSize / screen.y);
 
                     oldTouchPositions[0] = newTouchPositions[0];
                     oldTouchPositions[1] = newTouchPositions[1];
                     oldTouchVector = newTouchVector;
                     oldTouchDistance = newTouchDistance;
                 }
             }
 
         }
 
 
         
 
         if (HasReachedBounds())
         {
             Debug.Log("has reached left sprite bounds");
         }
     }
 
     bool HasReachedBounds()
     {
         var vertExtent = camera.orthographicSize;
         var horzExtent = vertExtent * Screen.width / Screen.height;
 
         if (transform.position.x - horzExtent < spriteMin.x)
         {
             boundReached = BoundReached.left;
             return true;
         }
         else if (transform.position.x + horzExtent > spriteMax.x)
         {
             boundReached = BoundReached.right;
             return true;
         }
         else if (transform.position.y - vertExtent < spriteMin.y)
         {
             boundReached = BoundReached.bottom;
             return true;
         }
         else if (transform.position.y + vertExtent > spriteMax.y)
         {
             boundReached = BoundReached.top;
             return true;
         }
         else
         {
             return false;
         }
 
 
     }
 }
 
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.

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

157 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 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

Camera only panning on 1 axis 1 Answer

Losing Focus When Zooming In And Out 1 Answer

Zooming cam by finger gestures 0 Answers

Camera zooming in and out 1 Answer

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