使用捏合/缩放拖动触摸输入冲突

时间:2017-06-30 16:36:28

标签: android unity3d touch

以下是我用于拖放相机的代码的一些部分。首先,我用两根手指缩小,然后释放其中一根手指。但是当我拖动剩余的手指时,相机会跳到最后一个手指位置。

void Update()
{
    if (Input.touchCount == 1)
    {
        Touch currentTouch = Input.GetTouch(0);
        if (currentTouch.phase == TouchPhase.Began)
        {
            hit_position = currentTouch.position;
            camera_position = Camera.main.transform.position;
        }

        if (currentTouch.phase == TouchPhase.Moved)
        {
            current_position = currentTouch.position;
            LeftMouseDrag()
        }

    }

    // if two fingers are touching the screen at the same time ...
    else if (Input.touchCount == 2)
    {
        isDragging = false;
        Touch touch1 = Input.touches[0];
        Touch touch2 = Input.touches[1];

        etc...


void LeftMouseDrag()
{
    Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
    direction = direction * -1;
    Vector3 position = camera_position + direction * panSpeed;
    Camera.main.transform.position = new Vector3(position.x, initialCameraHeight, position.z);
}

1 个答案:

答案 0 :(得分:0)

一个解决方案是在放置第二根手指后将bool设置为true(捏开已经开始)。然后,您可以阻止拖动方法发生,直到收缩结束,或者触摸次数重置为0(bool为假)。

void Update()
{
    if (Input.touchCount == 0)
    {
        isPinching = false;
    }
    if (Input.touchCount == 1 && isPinching == false)
    {
        Touch currentTouch = Input.GetTouch(0);
        if (currentTouch.phase == TouchPhase.Began)
        {
            hit_position = currentTouch.position;
            camera_position = Camera.main.transform.position;
        }

        if (currentTouch.phase == TouchPhase.Moved)
        {
            current_position = currentTouch.position;
            LeftMouseDrag()
        }

    }

    else if (Input.touchCount == 2)
    {
        isPinching = true;
        isDragging = false;
        Touch touch1 = Input.touches[0];
        Touch touch2 = Input.touches[1];

        //etc...
    }
}