简体   繁体   中英

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.

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.

When a touch occurs you can use the Input.GetTouch(0) to get a Touch struct. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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