松开 touch0 时缩放返回到最后的缩放值

问题描述 投票:0回答:0

当我释放第一个触摸输入(触摸 0 )时,正交缩放将返回到旧的缩放,而不是停留在当前缩放。但是当我释放第二个输入(触摸 1)时,它会保持当前缩放(Unity 2D C#)

public List<Vector2> area;
public AnimationCurve curve;
public float oldTouchDistance = 0;

    void Update()
    {
        if (Input.touchCount == 2)
            {
                        // get current touch positions
                        Touch tZero = Input.GetTouch(0);
                        Touch tOne = Input.GetTouch(1);

                        if (tOne.phase == TouchPhase.Began)
                        {
                            oldTouchDistance = Vector2.Distance(tZero.position, tOne.position);
                        }

                        float currentTouchDistance = Vector2.Distance(tZero.position, tOne.position);
                        // get offset value
                        float deltaDistance = oldTouchDistance - currentTouchDistance;
                        // set and clamp zoom
                        Camera.main.orthographicSize += deltaDistance * -.004f * curve.Evaluate(Camera.main.orthographicSize);
                        Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, .2f, 5);
                        // Fix cam pos
                        var zoom = Camera.main.orthographicSize;
                        var newCamPos = Camera.main.transform.position;
                        zoom = .2f - zoom * .04f;
                        Camera.main.transform.position = new Vector3(Mathf.Clamp(newCamPos.x, area[0].x * zoom, area[1].x * zoom), Mathf.Clamp(newCamPos.y, area[0].y * zoom, area[1].y * zoom), -10);
                        oldTouchDistance = currentTouchDistance;
            }
    }

如果 touchPhase 已结束或被取消,我尝试使脚本不运行,但由于某种原因它继续运行。

c# unity-game-engine unity-webgl
© www.soinside.com 2019 - 2024. All rights reserved.