Android - Multiple Touch Inputs

So I am trying to make a local multiplayer android game and I’m a bit confused here

This is my code now for player 1’s button (Player 2 is the same)

 public tank1 tank1t;
   
    void OnMouseDown()
    {
       
        tank1t.ifRotate = false;
        tank1t.Fire();
    }

    void OnMouseUp()
    {
      
      
       
        if (tank1t.rotatingDirection == 1)
        {
            tank1t.rotatingDirection = 2;
        }
        else if (tank1t.rotatingDirection == 2)
        {
            tank1t.rotatingDirection = 1;
        }
        tank1t.stopmov();
        tank1t.ifRotate = true;
       
    }

But player 1 and player 2 buttons cant be clicked at the same time
and I have no idea how to make it work the way I want to
I need that Touch Down/Touch up and I have no idea how to handle that with multiple touches.
If anyone can help me that would be amazing.

One approach people seem to use for multi touch buttons is EventTriggers:

I think the input does allow for multi touch - but it takes some custom settings to get it to work proper - for instance enabling multitouch through code (Input.MultiTouchEnabled = true; ) and some stuff.

And I think if you carefully manually handle touches with GetTouch you can also do multi touch setups:

EDIT: one more thing to consider - last I checked unity remote does not accept multi touch properly, and you’ll need to test a build on the actual device rather than using remote!

1 Like

You’ll need to look into touch input (there are checks for touch which you would look for in Update) and not use the buttons as buttons.

I honestly haven’t tried this, but this would be my guess as I don’t believe the MouseDown and MouseUp can handle multiple touches properly.

Example would be getting a touch from the array of touches Unity - Scripting API: Input.GetTouch and then checking for when that touch ends and seeing if it entered and exited a gui element.

2 Likes

So i came up with this code:

public Text text;

    // Update is called once per frame
    void Update () {
        if(Input.touchCount > 0)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Vector2 touchpos = Input.touches[i].position;
                if(touchpos.y >= 400 && touchpos.x <= 450)
                {
                    text.text = touchpos.ToString();
                }
            }
            /*
            Touch touch = Input.GetTouch(0);
            Debug.Log(touch.position);
            text.text = touch.position.ToString();
            */
        }
      
    }

But the problem is that on every device the Y/X is different
Is that the right way to do this?

That is a tricky situation when it comes to scaling for all screens. There isn’t an ideal solution I’ve discovered - but you could depend on checking Screen.height and Screen.width to determine what scale you should be checking at. An alternative way might be to check with raycasts when a touch is down, and if it lands on a button, consider that a hit, here is an example of that kind of stuff:
https://stackoverflow.com/questions/49321922/unity-raycast-ui-button-and-call-its-on-click-event

That might not have working code - I haven’t personally tested, but google around for stuff similar to that, and see if you can hack something together.

Thank you!
But is it possible to use stuff such as OnTouchUp/OntTouchDown With this?

(Btw sry my English is really bad :confused: )

I’m actually not totally sure if there is way - but if so, google may know the answer :stuck_out_tongue:

There might be some way to setup delegates to call those methods when a certain raycast hits that button - but it would be a bit overkill, as you can already tell the button is being hit when the raycast detects it - so you probably wouldn’t need it to do that anymore. I hope that makes sense :smile:

Yeah it does make sense xd
But now i have this code:

 private float screenHeight;
    private float screenWidth;

    public tank1 tank1t;
    public tank2 tank2t;

    private void Start()
    {
        screenHeight = Screen.currentResolution.height;
        screenWidth = Screen.currentResolution.width;

        screenHeight = screenHeight / 2;
        screenWidth = screenWidth / 2;

    }
   
    
    void Update () {
        if(Input.touchCount > 0)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Vector2 touchpos = Input.touches[i].position;
                Touch touch = Input.touches[i];
                if (touchpos.y >= screenHeight && touchpos.x <= screenWidth)
                {
                   
                  
                    switch (touch.phase)
                    {
                        // Record initial touch position.
                        case TouchPhase.Began:
                            tank1t.ifRotate = false;
                            tank1t.Fire();
                            break;

                       
                        //case TouchPhase.Moved:
                          //  break;

                       
                        case TouchPhase.Ended:
                            if (tank1t.rotatingDirection == 1)
                            {
                                tank1t.rotatingDirection = 2;
                            }
                            else if (tank1t.rotatingDirection == 2)
                            {
                                tank1t.rotatingDirection = 1;
                            }
                            tank1t.stopmov();
                            tank1t.ifRotate = true;
                            break;
                    }
                }
            }
           
          
           
        }
      
    }

It all works fine but when I don’t release the touch and move to the side
It doesn’t really do anything till I touch again
I understand why this is happening but i have no idea how to fix it :frowning: