Pinch zoom for 3d unity model / webgl on mobile devices not working

43 Views Asked by At

I have a 3d model of a human for my physio diagnosis tool, in which the user selects areas of pain. the model works fine on desktop including zooming and rotating the model with the track pad. on mobile the rotation works fine however the 2 finger pinch zoom does not, it either does not work or is very buggy depending on the device.

using UnityEngine;

[AddComponentMenu("Camera-Control/Mouse Orbit with Zoom")]
public class MouseOrbitImproved : MonoBehaviour
{
    public Transform target;
    public float distance = 20.0f;
    public float xSpeed = 200.0f;
    public float ySpeed = 200.0f;

    public float yMinLimit = -20f;
    public float yMaxLimit = 80f;

    public float distanceMin = .5f;
    public float distanceMax = 15f;

    public float zoomSensitivity = 0.2f;

    private float x = 0.0f;
    private float y = 0.0f;
    private float initialTouchDistance;
    private float currentTouchDistance;

    // Use this for initialization
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        if (GetComponent<Rigidbody>())
        {
            GetComponent<Rigidbody>().freezeRotation = true;
        }
    }

    void LateUpdate()
    {
        if (target)
        {
            if (Application.isMobilePlatform)
            {
                // Touch input for mobile
                if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    Touch touch = Input.GetTouch(0);
                    x += touch.deltaPosition.x * xSpeed * 0.02f;
                    y -= touch.deltaPosition.y * ySpeed * 0.02f;
                }
                else if (Input.touchCount == 2)
                {
                    // Pinch zoom for mobile
                    Touch touchZero = Input.GetTouch(0);
                    Touch touchOne = Input.GetTouch(1);

                    if (touchZero.phase == TouchPhase.Began || touchOne.phase == TouchPhase.Began)
                    {
                        initialTouchDistance = Vector2.Distance(touchZero.position, touchOne.position);
                        currentTouchDistance = initialTouchDistance;
                    }
                    else if (touchZero.phase == TouchPhase.Moved || touchOne.phase == TouchPhase.Moved)
                    {
                        currentTouchDistance = Vector2.Distance(touchZero.position, touchOne.position);
                        float deltaDistance = currentTouchDistance - initialTouchDistance;

                        // Adjust the factor to change the zoom sensitivity and make it frame-rate independent
                        distance = Mathf.Clamp(distance - deltaDistance * zoomSensitivity * Time.deltaTime, distanceMin, distanceMax);
                        initialTouchDistance = currentTouchDistance;
                    }
                }
            }
            else
            {
                // Mouse input for web and other platforms
                if (Input.GetMouseButton(0))
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                    y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                }

                // Scroll wheel zoom
                distance -= Input.GetAxis("Mouse ScrollWheel") * 30 * zoomSensitivity;
            }

            y = ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);
            Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
            Vector3 position = rotation * negDistance + target.position;

            transform.rotation = rotation;
            transform.position = position;
        }
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        return Mathf.Clamp(angle, min, max);
    }
}

i think this has to do with my unity camera script which i have experimented with, but yet to solve the problem

0

There are 0 best solutions below