Detect last Touch

Hi, I’m making a mobile joystick from scratch, but when i try to use 2 joystick at the same time i don’t know how to detect what touch is used by the first joystick and what by the second… can you help me?

Hi @PhixDevelop ,
When you have 2 joysticks and one is on the left side and the other joystick ist on the right side, you could check if the position.x is on the left side or on the right side of the screen.


Here is a little code which may help you:

private Vector3 position;
    Vector3 joy1; // left joystick
    Vector3 joy2; // right joystick
    private float width;
    


    void Awake()
    {
        width = (float)Screen.width / 2.0f;
    }

    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            
            Vector2 pos = touch.position;
            pos.x = (pos.x - width) / width;
            pos.y = (pos.y - height) / height;
            position = new Vector3(-pos.x, pos.y, 0.0f);

            if(position.x < width/2){
                // left joystick
                joy1 = position;
            }else{
                // right joystick
                joy2 = position;
            }
            

            if (Input.touchCount == 2)
            {
                touch = Input.GetTouch(1);
                Vector2 pos = touch.position;
                pos.x = (pos.x - width) / width;
                pos.y = (pos.y - height) / height;
                position = new Vector3(-pos.x, pos.y, 0.0f);

                if(position.x < width/2){
                    // left joystick
                    joy1 = position;
                }else{
                    // right joystick
                    joy2 = position;
                }
                
            }
        }
    }

This script only check the two finger touch positions. Then it checks if the finger is on the right or on the left side of the screen.