Search Unity

multipule touch issue

Discussion in 'iOS and tvOS' started by rain330047, Jun 26, 2017.

  1. rain330047

    rain330047

    Joined:
    Jun 3, 2016
    Posts:
    10
    Hi, all:
    our APP made with unity(5.6.0), we need to scale the model by two fingers. And also we need to take a picture by calling image picker.
    However, once we did took a photo then we can't scale the model cuz APP can only detected first touch.
    The state of multipletouchenabled is true.

    Here is the flow:
    we scale the model by two fingers ------- works fine.
    take photo ---- get the texture data;
    we scale the model by two fingers ----- Only can detect first touch, seems like multipletouchenable is false, but the log show multipletoucheable is true!!!

    pls help
     
    Last edited: Jun 26, 2017
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Can you show your code (in code tags) as there's likely an logic error somewhere in your code.
     
  3. rain330047

    rain330047

    Joined:
    Jun 3, 2016
    Posts:
    10
    Code (CSharp):
    1. public class ScaleController : MonoBehaviour
    2. {
    3.     public float MinScale = 0.1f;
    4.     public float MaxScale = 0.5f;
    5.  
    6.     private InstantTrackerController _controller;
    7.     private Transform _activeObject = null;
    8.  
    9.     private Vector3 _touch1StartGroundPosition;
    10.     private Vector3 _touch2StartGroundPosition;
    11.     private Vector3 _startObjectScale;
    12.  
    13.     private void Start ()
    14.     {
    15.         _controller = GetComponent<InstantTrackerController>();
    16.     }
    17.  
    18.     private void Update ()
    19.     {
    20.         if (Input.touchCount >= 2)
    21.         {
    22.             Touch touch1 = Input.GetTouch(0);
    23.             Touch touch2 = Input.GetTouch(1);
    24.             Transform hitTransform;
    25.  
    26.             if (_activeObject == null)
    27.             {
    28.                 if (GetTouchObject(touch1.position, out hitTransform))
    29.                 {
    30.                     SetTouchObject(hitTransform);
    31.                 } else if (GetTouchObject(touch2.position, out hitTransform))
    32.                 {
    33.                     SetTouchObject(hitTransform);
    34.                 } else if (GetTouchObject((touch1.position + touch2.position) / 2, out hitTransform))
    35.                 {
    36.                     SetTouchObject(hitTransform);
    37.                 }
    38.  
    39.                 if (_activeObject != null)
    40.                 {
    41.                     _touch1StartGroundPosition = GetGroundPosition(touch1.position);
    42.                     _touch2StartGroundPosition = GetGroundPosition(touch2.position);
    43.                     _startObjectScale = _activeObject.localScale;
    44.                 }
    45.             }
    46.  
    47.             if (_activeObject != null)
    48.             {
    49.                 var touch1GroundPosition = GetGroundPosition(touch1.position);
    50.                 var touch2GroundPosition = GetGroundPosition(touch2.position);
    51.  
    52.                 float startMagnitude = (_touch1StartGroundPosition - _touch2StartGroundPosition).magnitude;
    53.                 float currentMagnitude = (touch1GroundPosition - touch2GroundPosition).magnitude;
    54.  
    55.                 _activeObject.localScale = _startObjectScale * (currentMagnitude / startMagnitude);
    56.              
    57.                 if (_activeObject.localScale.x < MinScale)
    58.                 {
    59.                     _activeObject.localScale = new Vector3(MinScale, MinScale, MinScale) ;
    60.                 }
    61.  
    62.                 if (_activeObject.localScale.x > MaxScale)
    63.                 {
    64.                     _activeObject.localScale = new Vector3(MaxScale, MaxScale, MaxScale);
    65.                 }
    66.             }
    67.         }
    68.         else
    69.         {
    70.             _activeObject = null;
    71.  
    72.         }
    73.     }
    74.  
    75.     private bool GetTouchObject(Vector2 touchPosition, out Transform hitTransform)
    76.     {
    77.         var touchRay = Camera.main.ScreenPointToRay(touchPosition);
    78.         touchRay.origin -= touchRay.direction * 100.0f;
    79.  
    80.         RaycastHit hit;
    81.         if (Physics.Raycast(touchRay, out hit, 400.0f, LayerMask.GetMask("Sofa")))
    82.         {
    83.             hitTransform = hit.transform.root;
    84.             return true;
    85.         }
    86.  
    87.         hitTransform = null;
    88.         return false;
    89.     }
    90.  
    91.     private Vector3 GetGroundPosition(Vector2 touchPosition)
    92.     {
    93.         var groundPlane = new Plane(Vector3.up, Vector3.zero);
    94.         var touchRay = Camera.main.ScreenPointToRay(touchPosition);
    95.         float enter;
    96.         if (groundPlane.Raycast(touchRay, out enter))
    97.         {
    98.             return touchRay.GetPoint(enter);
    99.         }
    100.         return Vector3.zero;
    101.     }
    102.  
    103.     private void SetTouchObject(Transform newObject)
    104.     {
    105.         Debug.Log("ScaleController.SetTouchObject  1");
    106.         // foreach (var obj in _controller.ActiveModels.Values)
    107.         foreach (SofaItem item in _controller.ActiveModels)
    108.         {
    109.             Debug.Log("ScaleController.SetTouchObject  2");
    110.             if (item.obj == newObject.gameObject)
    111.             {
    112.                 Debug.Log("ScaleController.SetTouchObject  3");
    113.                 _activeObject = newObject;
    114.                 Debug.Log(_activeObject.name);
    115.             }
    116.         }
    117.     }
    118. }
     
    Last edited: Jun 26, 2017
  4. rain330047

    rain330047

    Joined:
    Jun 3, 2016
    Posts:
    10
    thx for reply, above is my code.
     
  5. rain330047

    rain330047

    Joined:
    Jun 3, 2016
    Posts:
    10
    by the way, the app works fine on android.
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
  7. rain330047

    rain330047

    Joined:
    Jun 3, 2016
    Posts:
    10