Unity Input Touch when I release second touch it also cancels the first touch

Viewed 517

I am working on a metrovania style game and I identify touch areas for left-right movements and jump. The problem is; For example I am moving right and at the same time I jump, It works perfectly but when I release jump it also cancels the movement to the right. Is there anyway to continue Input.GetTouch(0) after releasing Input.GetTouch(1) ?

Thanks in advance

    Input.multiTouchEnabled = true;       
    
    if (Input.touchCount > 0)
    {    
        for ( int i = 0 ; i < tapCount ; i++ ) {
    Touch touch = Input.GetTouch(i);
        switch (touch.phase)  {

        case TouchPhase.Began:      

        
            if (touch.position.x < Screen.width /6  && touch.position.y < Screen.height / 4 && ){
            rb.velocity = new Vector2 (- moveSpeed , rb.velocity.y);                            

                      
           else if (touch.position.x > Screen.width /6 &&  touch.position.x < Screen.width /3 && touch.position.y < Screen.height / 4 ) {
            rb.velocity = new Vector2 (moveSpeed , rb.velocity.y);              
       
            
            else if (touch.position.x > Screen.width * 5/6 && touch.position.y < Screen.height / 4 && grounded == true ) {
            rb.AddForce(new Vector2(0, jumpForce));
1 Answers

Without more context of your code I can only answer this quite generic.

You are iterating over all touches so if one touch goes up, then of course it will trigger the according TouchPhase.Ended for any of them.

Also note in general from Touch.fingerId

All current touches are reported in the Input.touches array or by using the Input.GetTouch function with the equivalent array index. However, the array index is not guaranteed to be the same from one frame to the next. The fingerId value, however, consistently refers to the same touch across frames.

So this is what you should use to identify the touches between multiple frames.

This said you could store the id of the first received touch via e.g.

private int firstid;

private void Update ()
{
    if(Input.touchCount == 1)
    {
        var touch = Input.GetTouch(0);
        if(touch.phase == TouchPhase.Began)
        {
            firstId = touch.fingerId;
        }
    }

    // Later rather use this to iterate the touches
    var touches = Input.touches;

    for(var i = 0; i < touches.Length, i++)
    {
        var currentTouch = touches[i];

        // Now from here you can always check the 
        if(currentTouch.fingerId == firstId)
        {
            // treat this touch different 
        }
        else
        {
            // ...
        }
    }
} 
Related