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#