Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Proper Enabling of MultiTouch Code - JS

Discussion in 'iOS and tvOS' started by ippdev, Oct 12, 2014.

  1. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    I thought this script below would enable multitouch as it iterates through the array of touches on each Update loop. I am told it does not do so on iPhone and do not have a recent device to test it on (I own the first version of the iPad and iPhone and they won't update..the iphone reset itself to 1978 or 1969). Can someone point out what it is that needs adjusting.

    Code (JavaScript):
    1. var useMouse : boolean;
    2. var levelCtrl : LevelController;
    3. function Start () {
    4.  
    5. }
    6. function Update () {
    7.     // Code for OnMouseDown in the iPhone. Unquote to test.
    8.     var hit : RaycastHit;
    9.     if (!useMouse) {
    10.         for (var i = 0; i < Input.touchCount; ++i) {
    11.             if (Input.GetTouch(i).phase == TouchPhase.Began) {
    12.                 // Construct a ray from the current touch coordinates
    13.                 var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
    14.                 if (Physics.Raycast (ray,hit)) {
    15.                     print (hit.transform.gameObject);
    16.                     var hexagonName = hit.transform.gameObject.name;
    17.                     levelCtrl.FlipTileToObverse (hexagonName);
    18.                 }
    19.                }
    20.            }
    21.     } else if (Input.GetMouseButtonDown(0) && useMouse) {
    22.         ray = camera.ScreenPointToRay (Input.mousePosition);
    23.         if (Physics.Raycast (ray,hit)) {
    24.             //print (hit.transform.gameObject);
    25.             hexagonName = hit.transform.gameObject.name;
    26.             levelCtrl.FlipTileToObverse (hexagonName);
    27.         }
    28.     }
    29. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I'd be inclined to use conditional compilation:

    Code (csharp):
    1. #if UNITY_IPHONE
    2.    for (var i = 0; ...
    3.    }
    4. #else
    5.    if (Input.GetMouseButtonDown()) {
    6.    }
    7. #endif
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789

    Thanks Graham. I presume multitouch code was correct then. My guess then is that the client did not set the useMouse flag to false and the mouseDown was giving him single touch capabilities only. I will implement the above suggestion which will avoid user error. How do you put #if ANDROID on the same line as #if UNITY_IPHONE, or do i have to replicate the for loop?