- Home /
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#
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.
Ya I was thinking of something like that but last time I tried it absolutely failed.
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.
@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.
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;
}
could someone place the complete script please...i can't get it to work
thanks
Your answer
Follow this Question
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