Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 The-Designer-Penguin · Nov 19, 2014 at 08:54 AM · c#androidzoompinch

Mobile Zoom System, That Zooms In On The Right Position

Hey everybody I have made two separate scripts one for pinch to zoom:

 using UnityEngine;
 using System.Collections;
 
 public class PinchZoom : MonoBehaviour 
 {
     public float OrthographicZoomSpeed = 0.0625F;
     public float ZoomReset = 1;
     public GameObject childCamera;
 
     void Update()
     {
 
         if (Input.touchCount >= 2) 
             {
                 Touch TouchZero = Input.GetTouch (0);
                 Touch TouchOne = Input.GetTouch (1);
 
                 Vector2 TouchZeroPreviousPosition = TouchZero.position - TouchZero.deltaPosition;
                 Vector2 TouchOnePreviousPosition = TouchOne.position - TouchOne.deltaPosition;
 
                 float PreviousTouchDeltaMagnatude = (TouchZeroPreviousPosition - TouchOnePreviousPosition).magnitude;
                 float TouchDeltaMagnatude = (TouchZero.position - TouchOne.position).magnitude;
 
                 float DeltaMagnitudeDifference = PreviousTouchDeltaMagnatude - TouchDeltaMagnatude;
 
                 childCamera.camera.orthographicSize += DeltaMagnitudeDifference * (OrthographicZoomSpeed * childCamera.camera.orthographicSize);
 
                 if (childCamera.camera.orthographicSize <= 3.0F) 
                     {
                         childCamera.camera.orthographicSize = 3.0F;
                     }
                 if (childCamera.camera.orthographicSize >= 13.0F) 
                     {
                         childCamera.camera.orthographicSize = 13.0F;
                     }
             } 
         else if (childCamera.camera.orthographicSize <= 4.0F)
             {
                 childCamera.camera.orthographicSize = Mathf.Lerp(childCamera.camera.orthographicSize , 4.0F, Time.deltaTime/ZoomReset);
             }
     }
 }

and a panning script:

 using UnityEngine;
 using System.Collections;
 
 public class CameraMovement : MonoBehaviour 
 {
     public float speed = 0.1F;
     public GameObject childCameraObject;
 
     void Update()
     {
         if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Moved) 
         {
             Touch TouchZero = Input.GetTouch (0);
             Vector2 touchDeltaPosition = TouchZero.deltaPosition;
             childCameraObject.transform.Translate(-touchDeltaPosition.x * speed * Time.deltaTime,-touchDeltaPosition.y * speed * Time.deltaTime, 0);        
         }
     }
 }

and they work very well together although there is one problem that I have no idea how to fix no matter how much research I do, the problem is that no matter where the pinches into on the screen it always zooms into the centre of the screen know matter what. I know that is because I am using orthographic zoom, but because the camera in my scene is orthographic changing its local z position(the camera is isometric x=30,y=45,z=0 rotation)does not do anything? I am basically asking how would you pull this off? and could you please provide the basic code because I am new to c#

Comment
Add comment · Show 10
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 koray1396 · Nov 19, 2014 at 12:09 PM 0
Share

by a quick look, i can see that you only change the orthographic size of camera, you should also change the position, in order to zoom to location. you can add lerping for position just as you did for the size to pinchzoom script.

avatar image The-Designer-Penguin · Nov 21, 2014 at 05:37 AM 0
Share

Ya I was thinking of something like that but last time I tried it absolutely failed.

avatar image koray1396 · Nov 21, 2014 at 10:21 AM 0
Share

allright, here are the steps i can think of;

  • Get the position at the center of two touches, that would be, (touch[0] position + touch[1] position) / 2.

  • Convert it to world points, using Camera.main.ScreenToWorldPoint. This is your target position for the camera. You should also decide whether your initial target will change during pinch. Store the initial magnitude between touches, and initial position for the camera.

  • Define a $$anonymous$$axDelta$$anonymous$$agnitudeDifference, apply lerping such as Vector3.Lerp(initialPos, targetPos, $$anonymous$$athf.Clamp(currentDelta$$anonymous$$agnitude / $$anonymous$$axDelta$$anonymous$$agnitude,0,1));

There might be errors but my general idea would be like this.

avatar image The-Designer-Penguin · Nov 22, 2014 at 02:31 AM 0
Share

Ill give it a try

avatar image NoseKills · Nov 22, 2014 at 09:27 AM 0
Share

@koray1396 is definitely on the right track. You want to store the world position between youR fingers when you start and keep moving the camera so that the same world position stays in the screen position between your fingers at all times.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lav-patel · Nov 28, 2014 at 07:18 AM

Zoom In Effect for Orthographic Camera using iTween.

 void Update () 
 {

     if (Input.GetMouseButtonDown (0))
     {

 // camera will move with orthogaphic cam size
 // orthographic camera size  
 //from = current orthographic camsize
 //to = end of zoom in point
 iTween.ValueTo (gameObject,iTween.Hash("from",5.35f,"to",2.9f,"time",10f,"onupdate","camzoom")); 
 // camera poistion move
 iTween.ValueTo (gameObject,iTween.Hash("from",gameObject.transform.position,"to",new Vector3(-3.569774f,-1.567216f,gameObject.transform.position.z),"time",10f,"onupdate","newpos"));


     }

}

 void newpos(Vector3 pos)
 {
     gameObject.transform.position = pos;
 }
 void camzoom(float val)
 {
     Camera.main.orthographicSize = val;
 }
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 Arno1975 · Jan 06, 2017 at 09:58 AM 0
Share

could someone place the complete script please...i can't get it to work

thanks

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

6 People are following this question.

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

Related Questions

How to centre a 3D pinch zoom 1 Answer

easiest way to do pinch zoom? 5 Answers

Multiple Cars not working 1 Answer

Pinch to zoom acting weird on Android 1 Answer

Distribute terrain in zones 3 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