简体   繁体   English

如何在Unity中阻止多点触控跳转

[英]How to Block Multi-Touch Jumps in Unity

My problem is that I control an object on Y Axis by touching, but when I multi-touch on the screen and withdraw the first touching, the game object jumps from first touch position to the second touching position. 我的问题是我通过触摸控制Y轴上的对象,但是当我在屏幕上进行多点触摸并撤回第一次触摸时,游戏对象会从第一次触摸位置跳到第二次触摸位置。

I want to that even though the player withdraws the first touch, object's movement continues like the first touch. 我希望即使玩家撤消了第一触,物体的移动仍会像第一触一样继续。

How can I solve it? 我该如何解决?

My code: 我的代码:

void Update () {

        if (die == true)
            return;
        if(Input.GetMouseButtonUp(0)){
            point = transform.position;
            click = false;
        }
        if (Input.GetMouseButton(0)) {
            ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); 
            last = ray.GetPoint(1);
            if(click==false){
                click=true;
                first = last;
            }
            transform.position  = new Vector2(0, point.y + last.y -  first.y );
        }

    }

As SteakOverlow mentioned, if you use touch specific code you a lot more helpful methods to work with. 正如SteakOverlow提到的,如果您使用触摸特定的代码,则可以使用许多有用的方法。

When a touch occurs you can use the Input.GetTouch(0) to get a Touch struct. 发生触摸时,可以使用Input.GetTouch(0)获取Touch结构。 This struct contains a lot of useful information about the touch. 此结构包含许多有关触摸的有用信息。 For instance, fingerID to let you know which finger is used and phase, which lets you know which touch state the finger is currently in. If the phase is "ended" then you can store the latest position of the finger and then let the next finger continue the operation with the offset from the last finger to the new finger. 例如,fingerID可以让您知道使用了哪个手指以及其相位,它可以让您知道手指当前处于哪种触摸状态。如果相位为“结束”,则可以存储手指的最新位置,然后让下一手指手指从最后一个手指到新手指的偏移继续操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM