UNITY-WebGL的多点触控问题(相机缩放)

时间:2018-12-12 15:20:13

标签: unity3d pinchzoom unity-webgl

我正在尝试实现一种可以旋转和放大场景的简单轨道骆驼。我遵循了一些教程,如果将其构建为PC Standalone,则可以在Windows上运行程序而没有任何问题。如果我将其构建为WebGL,则我的相机仍然可以通过单点触摸(旋转)正常工作,而在多点触摸下,我的行为会很奇怪。很难解释,但是即使我不动手指也无法放大和缩小。是“模糊”之王。我测试过的浏览器是Firefox和Chrome。

这是我的代码。

using System.Collections; 
using System.Collections.Generic;
using UnityEngine;

 public class CameraOrbitTouch : MonoBehaviour {

     public void Init()
{

    //If there is no BeginMenu, create a temporary target at 'distance' from the cameras current viewpoint
    if (!target)
    {
        GameObject go = new GameObject("Cam Target");
        go.transform.position = transform.position + (transform.forward * distance);
        target = go.transform;
    }

    distance = Vector3.Distance(transform.position, target.position);
    currentDistance = distance;
    desiredDistance = distance;

    //be sure to grab the current rotations as starting points.
    position = transform.position;
    rotation = transform.rotation;
    currentRotation = transform.rotation;
    desiredRotation = transform.rotation;

    xDeg = Vector3.Angle(Vector3.right, transform.right);
    yDeg = Vector3.Angle(Vector3.up, transform.up);

}

/*
 * Camera logic on LateUpdate to only update after all character movement logic has been handled.
 */
void LateUpdate()
{ 

        if (Input.touchCount == 2)
        {
            //First finger touch
            Touch touchZero = Input.GetTouch(0);
            //Second finger touch
            Touch touchOne = Input.GetTouch(1);
            //Position shift 1st finger - right
            Vector2 touchZeroPreviousPosition = touchZero.position - touchZero.deltaPosition;
            //Position shift 2nd finger - right
            Vector2 touchOnePreviousPosition = touchOne.position - touchOne.deltaPosition;

            float prevTouchDeltaMag = (touchZeroPreviousPosition - touchOnePreviousPosition).magnitude;
            float TouchDeltaMag = (touchZero.position - touchOne.position).magnitude;
            //Total shift
            float deltaMagDiff = prevTouchDeltaMag - TouchDeltaMag;

            desiredDistance += deltaMagDiff * Time.deltaTime * zoomRate * 0.0025f * Mathf.Abs(desiredDistance);
        }


        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            Vector2 touchposition = Input.GetTouch(0).deltaPosition;
            xDeg += touchposition.x * xSpeed * 0.002f;
            yDeg -= touchposition.y * ySpeed * 0.002f;
            yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);

        }

        desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
        currentRotation = transform.rotation;
        rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
        transform.rotation = rotation;


        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
        position = target.position - (rotation * Vector3.forward * currentDistance);
        position = position - targetOffset;
        transform.position = position;
}

//Max rotation of the camera around the object (360 sphere)
private static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp(angle, min, max);
}

 }

0 个答案:

没有答案