Search code examples
c#unity-game-enginetouchmulti-touch

multi-touch in the unity game not working


I am following this video here

The tutorial and everything works fine except the multi-touch parts. I have two buttons that are actually two different images on the canvas but when I use one I can't use the other touch button. Each one of my buttons has this script.

Here is the code:

    using UnityEngine.UI;
    using UnityEngine.EventSystems;
    using System.Collections;

    public class SimpleTouchShoot : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
      private bool touched;
      private int pointerID;
      private bool canFire;

     void Awake()
     {
         touched = false;
         canFire = false;
     }

     public void  OnPointerDown (PointerEventData data)
     {
         if(!touched)
         {
             touched = true;
             pointerID = data.pointerId;
             canFire = true;
         }

     }

     public void OnPointerUp (PointerEventData data)
     {
         if(data.pointerId == pointerID)
         {
             touched = false;
             canFire = false;
         }

     }

     public bool CanFire ()
     {
         return canFire;
     }
 }

The version of Unity is 4.6.


Solution

  • You're using the Interface for the Event Handlers. It doesn't implement its own multi touch function so you would have to make your own touch logic. In most cases, it is a hacky way of doing it. Making an array or a list of touch inputs and having cases for it.

    Unity3D Forum - Event System

    Click on the link and check the Answer number 5. The answer is an hacky way of doing it. Because the Event Interface is adding EventHandler to whatever GameObject you have for the script. But it you would always need to tell the game engine that you accept more than one input <- to make it simple. Catch the 2nd touch input that is being ignored.