How to move and fire at same time with touchscreens? ( multitouch)

Hello Guys,

Could you explain how works goes with the multitouches? I want to move player object with one finger by touch joystick and with the others make fire by tapping ground. But when i move the player and fire with other finger at the same time, it does not perceive the tapping ground activity and this second touch affects on the joystick knob position. I think, i must distinguish the touch on the joystick object, from the other touches at the screen and look only that touch for moving action. How can i do that?

Firstly, this has been answered many times in many places, including several other answers on this site, but anyway for the sake of learning here we go!

One thing you must do is enable multi touch input. In your input script (where your listening for button presses or touchscreen taps and whatnot) you will put into Start() or Awake() this code:

Input.multiTouchEnabled = true;

And then when your checking for input you need to check for more than just a single touch. Something like this:

int tapCount = Input.touchCount;
 for ( int i = 0 ; i < tapCount ; i++ ) {
       Touch touch = Input.GetTouch(i);
       //Do whatever you want with the current touch.

And so from this point it all depends on what your game is setup like, maybe you need to check the position of that touch and see if it is over the joystick. Then if it is, and no other touch is already interacting with the joystick, have this touch move the joystick. If it doesn’t show up over the joystick, maybe it hits a button, or touches the screen where there are no buttons and you should raycast to see what gameobject they are tapping!

Good luck!