Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
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.

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

Camera only panning on 1 axis 1 Answer

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

2D Infinite Runner - Pan camera by screen height 1 Answer

Zoom camera based on position between player an object 2 Answers

How can I zoom out a camera locked on the player? 0 Answers


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