- Home /
Question by
Voky · Aug 16, 2016 at 05:06 AM ·
c#cameracamera-movementzoom
Pinch zoom
Hello, I am making pinch zoom with the still ground, which means, that camera size is increasing together with Y position.
Like on these pictures:
There is a code:
public class PinchZoom : MonoBehaviour
{
public float perspectiveZoomSpeed = 0.5f;
public float orthoZoomSpeed = 0.5f;
public float moveSpeed;
private float fakeMagnitudeDiff;
void Update()
{
if (Input.touchCount == 2) {
Touch touchZero = Input.GetTouch (0);
Touch touchOne = Input.GetTouch (1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
if (GetComponent<Camera>().orthographic) {
GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
GetComponent<Camera> ().orthographicSize = Mathf.Max (GetComponent<Camera> ().orthographicSize, 0.1f);
}
if(GetComponent<Camera> ().orthographicSize == 0.1f) {
fakeMagnitudeDiff = 0;
}
if (GetComponent<Camera> ().orthographicSize > 0.1f) {
fakeMagnitudeDiff = deltaMagnitudeDiff;
}
transform.position = new Vector3 (transform.position.x, transform.position.y + (fakeMagnitudeDiff * moveSpeed), -10);
}
}
}
At the first look everything works fine, till player try to do zoom in and zoom out a few times, because it will desynchronize Y position of camera. I think this could be caused by delay of frames, so I want to ask if it is even posible do this feature on this script, or if I must do new pinch zoom script if I want to do this feature. Please help...
bez-nazvu.png
(144.6 kB)
bez-nazvu2.png
(189.8 kB)
Comment