Handling seperate touches on mobile

Hi folks,

New to all this stuff–unity, programming, etc. I’m trying to write, what I feel like should be, a fairly easy script to control horizontal movement with one touch on the left side of the screen and jumping with a swipe on the right side of the screen.

Right now, the script allows you to move a game object left and right without problem, but when you swipe up to jump it doesn’t do what I’d like. It is fairly obvious to me why this is–when the second touch begins, the touchOrigin gets set to the position at the new touch.began, then when you finish the swipe the currentPosition jumps back to the first touch’s position setting difference to a large negative number and moving the object left even if you were moving right.

I guess my question is, how should I be handling multiple touches separately? I have read about fingerID, but I haven’t really grasped how to use effectively.

Any tips would be appreciated. Sorry for the clunky, less-than-graceful code… still learning. The code script I’m using is below.

using UnityEngine;
using System.Collections;

public class TouchControls : MonoBehaviour {

    private Vector2 touchOrigin = -Vector2.one;
    private Vector2 touchCurrent;
    public float speed;
    public float magnitude;
    public float jump;
    public Transform groundCheck;
    public bool grounded = false;
    float groundRadius = .1f;
    public LayerMask whatIsGround;
    bool facingRight = true;
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update ()
    {

        //Ground check.
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

        //Check if Input has registered more than zero touches
        if (Input.touchCount > 0)
        {
            foreach(Touch T in Input.touches)
            {

                //Check if the phase of that touch equals Began
                if (T.phase == TouchPhase.Began)
                {
                    //If so, set touchOrigin to the position of that touch
                    touchOrigin = T.position;
                }

                else if (T.phase == TouchPhase.Moved || T.phase == TouchPhase.Stationary)
                {
                    //Store the first touch's current position every frame.
                    touchCurrent = T.position;

                    //Store the difference between first touch's current position and first touch's original position every frame.
                    var difference = touchCurrent - touchOrigin;


                    //if touch is on the left half of screen...
                    if (T.position.x < Screen.width / 2 && Mathf.Abs(difference.x) > Mathf.Abs(difference.y))
                    {
                        if (Mathf.Abs(difference.x) > 15)
                        {
                            transform.Translate(new Vector2(Mathf.Clamp(difference.x, -60, 60) * speed, 0));
                            if (difference.x > 0 && !facingRight)
                            {
                                Flip();
                            }

                            else if (difference.x < 0 && facingRight)
                            {
                                Flip();
                            }
                        }
                    }
                  
                    //if touch is on the right half of screen and the movement of touch on the y axis is greater than that of the x and if ground...
                    else if (T.position.x > Screen.width / 2 && Mathf.Abs(difference.x) < Mathf.Abs(difference.y) && grounded)
                    {
                        //check the magnitude of the swipe
                        if (T.deltaPosition.magnitude > magnitude)
                        {
                            GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jump));
                            grounded = false;
                        }
                    }
                }

                  

                if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
                {
                }
            }
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

bump… any love?

Here we had a little post about Touches and fingerID

I don’t how i would do it, if with fingerID locked to a movement und released from the movement when you release touch or just check if touch is leftscreen, then call method (stearing) or when its on other screen side do jump stuff.

but you shouldn’t use same variables for different movements. so you wont have the problem of wrong values.

Thanks for the reply! For anyone interested, I decided to use a hashtable to store the position of each touch at fingerID of that touch in the hashtable. Worked great for what I needed.