Is there a way for my touch input to only detect the most recent touch?

So both inputs work when they are done individually, but if both are pressed simultaneously then they cancel each other out. I would like if only the most recent touch would work so that they don’t cancel out. Here is my code:

	private void Start () {
        rb = GetComponent<Rigidbody2D>();
        moveSpeed = 11f;
        verticleSpeed = -15f;
	}
	

	private void Update () {
		if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    if (touch.position.x < Screen.width / 2)
                        rb.velocity = new Vector2(-moveSpeed, verticleSpeed);
                    if (touch.position.x > Screen.width / 2)
                        rb.velocity = new Vector2(moveSpeed, verticleSpeed);
                    break;

                case TouchPhase.Ended:
                    rb.velocity = new Vector2(0f, verticleSpeed); //if it still resets the speed put -20
                    break;
            }
        }

This is a common error when dealing with touches. Each touch has a fingerId which actually identifies a finger from the moment it got pressed down (TouchPhase.Began) until the touch ended (TouchPhase.Ended or TouchPhase.Canceled).

If you only want to react to the latest touch you have to iterate through all touches and store the fingerID of the last “began” event. When you get a touch “Ended” you only set speed to 0 when the right last finger is released.

int lastFingerId = -1;
private void Update ()
{
    foreach (Touch touch in Input.touches)
    {
        switch (touch.phase)
        {
            case TouchPhase.Began:
                if (touch.position.x < Screen.width / 2)
                    rb.velocity = new Vector2(-moveSpeed, verticleSpeed);
                else
                    rb.velocity = new Vector2(moveSpeed, verticleSpeed);
                lastFingerId = touch.fingerId;
                break;
            case TouchPhase.Ended:
                if (touch.fingerId == lastFingerId)
                    rb.velocity = new Vector2(0f, verticleSpeed); //if it still resets the speed put -20
                break;
        }
    }
}

This way releasing the second last finger will not have any effect.

Can’t you just… Touch touch = Input.GetTouch(Input.touchCount - 1)