Home > OS >  PinchToZoom functionality does not work C# Unity
PinchToZoom functionality does not work C# Unity

Time:01-11

In the app, which displays objects from a bird's eye view, recognizes the touch as well as the finger movement but the zoom functionality remains off.

So in the end nothing happens except that the fingers and the movements are recognized.

I don't understand why this is happening.

using UnityEngine;




public class ViewerCameraController : MonoBehaviour
{
    private Camera cam;
    private RaycastHit raycastHit;
    public Collider col;
    public List<GameObject> objects;


    Vector3 touchStart;
    public float zoomOutMin = 1;
    public float zoomOutMax = 8;



    private void Awake()
    {

    }

    // Start is called before the first frame update
    void Start()
    {
        cam = GetComponent<Camera>();

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        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 prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;

            zoom(difference * 2.01f);
        }
        else if (Input.GetMouseButton(0))
        {
            Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Camera.main.transform.position  = direction;
        }
        zoom(Input.GetAxis("Mouse ScrollWheel"));
    }

    void zoom(float increment)
    {
        Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment, zoomOutMin, zoomOutMax);
    }
}```

CodePudding user response:

It is because you are not incrementing your camera zoom's "increment" value. You can also multiply the increment with speed as you need. I have also added the zoomMin and zoomMax function inside the Update() which is necessary. I have updated your script.

I did not understand why you are multiplying your difference with a higher value so in my script I have removed that - zoom (difference, zoomSpeed);

    private Camera cam;
    private RaycastHit raycastHit;
    public Collider col;
    public List<GameObject> objects;

    Vector3 touchStart;
    public float zoomOutMin = 1;
    public float zoomOutMax = 170;

    float zoomSpeed = 2.0f;

    private void Awake () {

    }

    // Start is called before the first frame update
    void Start () {
        cam = GetComponent<Camera> ();

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            touchStart = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        }
        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 prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - prevMagnitude;

            zoom (difference, zoomSpeed);
        } else if (Input.GetMouseButton (0)) {
            Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint (Input.mousePosition);
            Camera.main.transform.position  = direction;
        }
        zoom (Input.GetAxis ("Mouse ScrollWheel"), zoomSpeed);

        if (cam.fieldOfView < zoomOutMin) {
            cam.fieldOfView = zoomOutMin;
        } else
        if (cam.fieldOfView > zoomOutMax) {
            cam.fieldOfView = zoomOutMax;
        }
    }

    void zoom (float increment, float speed) {
        cam.fieldOfView  = increment * speed;
        cam.fieldOfView = Mathf.Clamp (cam.fieldOfView, zoomOutMin, zoomOutMax);
    }

CodePudding user response:

The problem was a small setting in Unity itself, the camera used was not set to Othographic....

enter image description here

  •  Tags:  
  • Related