Search Unity

[RESOLVED]Touch is not detecting multiple direction on one go

Discussion in 'Scripting' started by ma.parizeau, Sep 14, 2014.

  1. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Hi,

    I'm trying to detect the movement the player is doing. If I do one direction at a time, they all get captured properly.

    But if I do more then one, it will probably work 1 every 10 tries... no very reliable.
    I've tried putting this code in Touch.Moved and in Touch.Ended. So what's wrong? My code? Or is The touch library too slow to detect all movement?

    Code (CSharp):
    1.  
    2. Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
    3.  
    4. normalized = new Vector2(position.x - startPos.x, position.y - startPos.y);
    5. normalized.Normalize();
    6.  
    7. //determine the direction it's going
    8. if (normalized.x < -0.9f && (normalized.y > -0.2f&& normalized.y < 0.2f))
    9. {
    10.     Debug.Log("going left");
    11. }
    12. else if (normalized.x > 0.9f && (normalized.y > -0.2f&& normalized.y < 0.2f))
    13. {
    14.     Debug.Log("going right");
    15. }
    16. else if (normalized.y > 0.9f && (normalized.x > -0.2f&& normalized.x < 0.2f))
    17. {
    18.     Debug.Log("going up");
    19. }
    20. else if (normalized.y < -0.9f && (normalized.x > -0.2f&& normalized.x < 0.2f))
    21. {
    22.     Debug.Log("going down");
    23. }
    24. else
    25. {
    26.     Debug.Log("Change of direction");
    27.     startPos = new Vector3 (position.x, position.y, 0.0f);
    28. }
    29.  
    Thanks,
     
  2. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Hint: Your code is running like that
    Code (csharp):
    1. Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
    2. normalized = new Vector2(position.x - startPos.x, position.y - startPos.y);
    3. normalized.Normalize();
    4. //determine the direction it's going
    5. if (normalized.x < -0.9f && (normalized.y > -0.2f&& normalized.y < 0.2f))
    6. {
    7.   Debug.Log("going left");
    8. }
    9. else if (normalized.x > 0.9f && (normalized.y > -0.2f&& normalized.y < 0.2f))
    10.    {
    11.      Debug.Log("going right");
    12.    }
    13.    else if (normalized.y > 0.9f && (normalized.x > -0.2f&& normalized.x < 0.2f))
    14.      {
    15.        Debug.Log("going up");
    16.      }
    17.        else if (normalized.y < -0.9f && (normalized.x > -0.2f&& normalized.x < 0.2f))
    18.          {
    19.            Debug.Log("going down");
    20.          }
    21.          else
    22.          {
    23.            Debug.Log("Change of direction");
    24.            startPos = new Vector3 (position.x, position.y, 0.0f);
    25.          }
    In this case I would discard all else(if i am seeing it right you have got each case covered) or reorder the code a bit. Otherwise startPos won't get new values.
    Code (csharp):
    1. Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
    2. normalized = new Vector2(position.x - startPos.x, position.y - startPos.y);
    3. normalized.Normalize();
    4. //determine the direction it's going
    5. if((normalized.y > -0.2f && normalized.y < 0.2f)
    6. {
    7.    if (normalized.x < -0.9f )
    8.    {
    9.      Debug.Log("going left");
    10.    } else if (normalized.x > 0.9f)
    11.    {
    12.      Debug.Log("going right");
    13.    }
    14. }
    15. if(normalized.x > -0.2f&& normalized.x < 0.2f)
    16. {
    17.    if (normalized.y > 0.9f)
    18.    {
    19.      Debug.Log("going up");
    20.    }  else if (normalized.y < -0.9f)
    21.    {
    22.      Debug.Log("going down");
    23.    }
    24. }
    25. Debug.Log("Change of direction");
    26. startPos = new Vector3 (position.x, position.y, 0.0f);
     
  3. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Thanks, I will give it a try and keep you posted.
     
  4. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Hi Kogar,

    I've put your code in, did some testing. It's a lot better then it was... but it's still not 100%... it's about at 85% accurate.

    Because let's says I do a right, up, right, down in one swipe, sometimes it will only give me right, down. It's like if the player does it too quickly, it can't determine the directions. But if I do it slowly... it works 99.9% of the time.

    So now, I have to make a decision, do I put in this mechanic with it's flaws or do I go with a single direction type control...

    thanks for your help
     
  5. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Glad to hear.
    Maybe instead of position and startpos you can directly use deltaPosition to input into your normalized variable.
    http://docs.unity3d.com/ScriptReference/Touch-deltaPosition.html
    Maybe it will help and increase the accuracy over a shorter timespan. You just have to be careful that the y value is inverted from a ScreenToWorldPoint
     
  6. ma.parizeau

    ma.parizeau

    Joined:
    Mar 21, 2010
    Posts:
    116
    Here is what I ended up doing... taking into accounts the scenarios that I would accept. Also, I want to keep the startPos and endPos to know if we swiped or not.

    Code (CSharp):
    1. // Look for all fingers
    2. for (int i = 0; i < Input.touchCount; i++)
    3. {
    4.     Touch touch = Input.GetTouch(i);
    5.  
    6.     if (touch.phase == TouchPhase.Began)
    7.     {
    8.         ResetVariables();
    9.  
    10.         startPos = Camera.main.ScreenToWorldPoint(touch.position);
    11.     }
    12.     else if (touch.phase == TouchPhase.Moved)
    13.     {
    14.         //drawing trail
    15.         var position = Camera.main.ScreenToWorldPoint(touch.position);
    16.         trail.transform.position = new Vector3(position.x, position.y, 0.0f);
    17.         var delta = touch.position - touch.deltaPosition;
    18.         allCoordinates.Add(delta);
    19.     }
    20.     else if (touch.phase == TouchPhase.Ended)
    21.     {
    22.         //get direction and distance
    23.         endPos = Camera.main.ScreenToWorldPoint(touch.position);
    24.         distance = Vector3.Distance(startPos, endPos); //StartPos variable taken in the TouchPhase.Began phase
    25.      
    26.         //do we have a swipe or not?
    27.         if (distance > minDistance) //minDistance set to 10.0f
    28.         {
    29.             foreach (Vector2 points in allCoordinates) //ArrayList where I take all of the coordinates in TouchPhase.Moved
    30.             {
    31.                 normalized = new Vector2(points.x - startPos.x, points.y - startPos.y);
    32.                 normalized.Normalize();
    33.                 //determine the direction it's going
    34.                 if (normalized.y > -0.2f && normalized.y < 0.2f)
    35.                 {
    36.                     if (normalized.x < -0.9f)
    37.                     {
    38.                         if (action[action.Count - 1].ToString() != "left")
    39.                         {
    40.                             action.Add("left");
    41.                             actionCoordinates.Add((Vector2)points);
    42.                         }
    43.                     }
    44.                     else if (normalized.x > 0.9f)
    45.                     {
    46.                         if (action[action.Count - 1].ToString() != "right")
    47.                         {
    48.                             action.Add("right");
    49.                             actionCoordinates.Add((Vector2)points);
    50.                         }
    51.                     }
    52.                 }
    53.                 if (normalized.x > -0.2f && normalized.x < 0.2f)
    54.                 {
    55.                     if (normalized.y > 0.9f)
    56.                     {
    57.                         if (action[action.Count - 1].ToString() != "up")
    58.                         {
    59.                             action.Add("up");
    60.                             actionCoordinates.Add((Vector2)points);
    61.                         }
    62.                     }
    63.                     else if (normalized.y < -0.9f)
    64.                     {
    65.                         if (action[action.Count - 1].ToString() != "down")
    66.                         {
    67.                             action.Add("down");
    68.                             actionCoordinates.Add((Vector2)points);
    69.                         }
    70.                     }
    71.                 }
    72.  
    73.                 startPos = new Vector3(points.x, points.y, 0.0f);
    74.             }
    75.  
    76.             //remove the first item in the arrayList
    77.             action.RemoveAt(0); // because i'm too lazy and didn't want a null value ABOVE, I fill it with a default value that I now remove
    78.  
    79.             if(action.Count > 0)
    80.             {
    81.                 Debug.Log(string.Format("action(s) : {0}", action.Count));
    82.                 //TEST :: validate our action and coordinates that were captured
    83.                 for (int x = 0; x < action.Count; x++)
    84.                 {
    85.                     ////action and coordinates
    86.                     //Debug.Log(string.Format("Action : {0} AT X: {1} / Y: {2}", action[x],
    87.                     //    ((Vector2)actionCoordinates[x]).x, ((Vector2)actionCoordinates[x]).y));
    88.  
    89.                     //just the action
    90.                     Debug.Log(string.Format("Action : {0}", action[x]));
    91.                 }
    92.  
    93.                 var buildActions = string.Empty;
    94.  
    95.                 if (action.Count == 1)
    96.                 {
    97.                     if (action[0].ToString() == "right" || action[0].ToString() == "left")
    98.                     {
    99.                         //you just wan to run
    100.                         buildActions = string.Format("Run : {0}", action[0]);
    101.                     }
    102.                     else if (action[0].ToString() == "up")
    103.                     {
    104.                         //jump in the rirection he is facing
    105.                         buildActions = "Jump in the direction and the position of the player";
    106.  
    107.                     }
    108.                     else
    109.                     {
    110.                         buildActions = "Invalid action please try again";
    111.                     }
    112.                 }
    113.                 else if (action.Count == 2)
    114.                 {
    115.                     if (action[0].ToString() == "up" && action[1].ToString() == "down") //TODO :: validate coordinates
    116.                     {
    117.                         //jump in the direction he is facing
    118.                         buildActions = "Jump in the direction and the position of the player";
    119.                     }
    120.                     else if (action[0].ToString() == "right" || action[0].ToString() == "left") //TODO :: validate coordinates
    121.                     {
    122.                         //you want to run + misdirection
    123.                         buildActions = string.Format("Run : {0} + Misdirection", action[0]);
    124.                     }
    125.                     else
    126.                     {
    127.                         buildActions = "Invalid action please try again";
    128.                     }
    129.                 }
    130.                 else if (action.Count == 3)
    131.                 {
    132.                     if (action[0].ToString() == "up" && action[2].ToString() == "down"
    133.                         && (action[1].ToString() == "right" || action[1].ToString() == "left"))
    134.                     {
    135.                         //you want the player to jump
    136.                         buildActions = string.Format("Jump : {0} AT X: {1} / Y: {2}", action[1],
    137.                             ((Vector2)actionCoordinates[0]).x, ((Vector2)actionCoordinates[0]).y);
    138.                     }
    139.                     else if ((action[2].ToString() == "right" || action[2].ToString() == "left")
    140.                    && action[0].ToString() == "up" && action[1].ToString() == "down")
    141.                     {
    142.                         //you want to jump then run
    143.                         buildActions = string.Format("Jump and Run : {0} AT X: {1} / Y: {2}", action[2],
    144.                             ((Vector2)actionCoordinates[0]).x, ((Vector2)actionCoordinates[0]).y);
    145.                     }
    146.                     else if ((action[0].ToString() == "right" || action[0].ToString() == "left")
    147.                && action[1].ToString() == "up" && action[2].ToString() == "down")
    148.                     {
    149.                         //you want to jump then run
    150.                         buildActions = string.Format("Jump and Run : {0} AT X: {1} / Y: {2}", action[0],
    151.                             ((Vector2)actionCoordinates[1]).x, ((Vector2)actionCoordinates[1]).y);
    152.                     }
    153.                     else if ((action[0].ToString() == "right" || action[0].ToString() == "left")
    154.                         && action[0].ToString() == action[2].ToString() && action[0].ToString() == "up")
    155.                     {
    156.                         //you want to run and then jump
    157.                         buildActions = string.Format("Run and Jump : {0} AT X: {1} / Y: {2}", action[0],
    158.                             ((Vector2)actionCoordinates[1]).x, ((Vector2)actionCoordinates[1]).y);
    159.                     }
    160.                     else
    161.                     {
    162.                         buildActions = "Invalid action please try again";
    163.                     }
    164.                 }
    165.                 else if (action.Count == 4)
    166.                 {
    167.                     if (action[0].ToString() == action[2].ToString()
    168.                         && action[1].ToString() == "up" && action[3].ToString() == "down")
    169.                     {
    170.                         //you want to run and then jump
    171.                         buildActions = string.Format("Run and Jump : {0} AT X: {1} / Y: {2}", action[0],
    172.                             ((Vector2)actionCoordinates[1]).x, ((Vector2)actionCoordinates[1]).y);
    173.                     }
    174.                     else if (action[1].ToString() == action[3].ToString()
    175.                         && action[0].ToString() == "up" && action[2].ToString() == "down")
    176.                     {
    177.                         //you want to jump first and them run
    178.                         buildActions = string.Format("Jump first and run : {0} AT X: {1} / Y: {2}", action[1],
    179.                             ((Vector2)actionCoordinates[0]).x, ((Vector2)actionCoordinates[0]).y);
    180.                     }
    181.                     else if ((action[0].ToString() == "right" || action[0].ToString() == "left")
    182.                         && (action[1].ToString() == "right" || action[1].ToString() == "left")
    183.                         && action[2].ToString() == "up" && action[3].ToString() == "down")
    184.                     {
    185.                         //you went one way then the other and jumped
    186.                         buildActions = string.Format("Misdirection then Run and Jump : {0} AT X: {1} / Y: {2}", action[1],
    187.                             ((Vector2)actionCoordinates[2]).x, ((Vector2)actionCoordinates[2]).y);
    188.                     }
    189.                     else
    190.                     {
    191.                         buildActions = "Invalid action please try again";
    192.                     }
    193.                 }
    194.                 else if (action.Count == 5)
    195.                 {
    196.                     if (action[0].ToString() == action[2].ToString() && action[0].ToString() == action[4].ToString()
    197.                         && action[1].ToString() == "up" && action[3].ToString() == "down")
    198.                     {
    199.                         //you want to run, jump and then run again
    200.                         buildActions = string.Format("Run, Jump and Run {0} AT X: {1} / Y: {2}", action[0],
    201.                             ((Vector2)actionCoordinates[1]).x, ((Vector2)actionCoordinates[1]).y);
    202.                     }
    203.                     else if (action[1].ToString() == action[3].ToString()
    204.                         && action[2].ToString() == "up" && action[4].ToString() == "down")
    205.                     {
    206.                         //you went one way then the other and jumped
    207.                         buildActions = string.Format("Misdirection then Run and Jump : {0} AT X: {1} / Y: {2}", action[1],
    208.                             ((Vector2)actionCoordinates[2]).x, ((Vector2)actionCoordinates[2]).y);
    209.                     }
    210.                     else if ((action[3].ToString() == "right" || action[3].ToString() == "left")
    211.                         && action[1].ToString() == "up" && action[2].ToString() == "down")
    212.                     {
    213.                         //you went down then jumped, then run, then misredirection
    214.                         buildActions = string.Format("Misdirection then Jump and Run : {0} AT X: {1} / Y: {2}", action[1],
    215.                             ((Vector2)actionCoordinates[2]).x, ((Vector2)actionCoordinates[2]).y);
    216.                     }
    217.                     else
    218.                     {
    219.                         buildActions = "Invalid action please try again";
    220.                     }
    221.                 }
    222.                 else if (action.Count == 6)
    223.                 {
    224.                     if (action[0].ToString() == action[2].ToString() && action[0].ToString() == action[4].ToString()
    225.                         && action[1].ToString() == "up" && action[3].ToString() == "down")
    226.                     {
    227.                         //you want to run, jump and then run again
    228.                         buildActions = string.Format("Ended with misdirection - Run, Jump and Run : {0} AT X: {1} / Y: {2}", action[0],
    229.                             ((Vector2)actionCoordinates[1]).x, ((Vector2)actionCoordinates[1]).y);
    230.                     }
    231.                     else if (action[1].ToString() == action[3].ToString()
    232.                         && action[2].ToString() == "up" && action[4].ToString() == "down")
    233.                     {
    234.                         //you went one way then the other and jumped
    235.                         buildActions = string.Format("Misdirection then Run, Jump and Run : {0} AT X: {1} / Y: {2}", action[1],
    236.                             ((Vector2)actionCoordinates[2]).x, ((Vector2)actionCoordinates[2]).y);
    237.                     }
    238.                     else
    239.                     {
    240.                         buildActions = "Invalid action please try again";
    241.                     }
    242.                 }
    243.                 else if (action.Count == 7) //last chance fot he user to get it right ;)
    244.                 {
    245.                     if (action[0].ToString() == action[2].ToString() && action[0].ToString() == action[4].ToString()
    246.                         && action[1].ToString() == "up" && action[3].ToString() == "down")
    247.                     {
    248.                         //you want to run, jump and then run again
    249.                         buildActions = string.Format("Ended with misdirection(s) - Run, Jump and Run : {0} AT X: {1} / Y: {2}", action[0],
    250.                             ((Vector2)actionCoordinates[1]).x, ((Vector2)actionCoordinates[1]).y);
    251.                     }
    252.                     else
    253.                     {
    254.                         buildActions = "Invalid action please try again";
    255.                     }
    256.                 }
    257.                 else
    258.                 {
    259.                     //invalid moves, visual feedback and after 2 misses, tutorial
    260.                     buildActions = "Invalid or too many action(s) please try again";
    261.                 }
    262.  
    263.                 //TODO :: Place code here to move player or gameobject and use
    264.                 //'endPos' to know where to stop moving or left or right coordinates
    265.                 //if there was a misdirection at the end (so you probably want to use a flag
    266.                 Debug.Log(buildActions); // the coordinates are for the jump to when to perform it. (use X from Jump and Y from the Player)
    267.             }
    268.             else
    269.             {
    270.                 //TODO :: Place code here to move player or gameobject
    271.                 Debug.Log("move to this location (use endPos)");
    272.             }
    273.         }
    274.         else if (distance <= minDistance)
    275.         {
    276.             //TODO :: Place code here to move player or gameobject
    277.             Debug.Log("move to this location (use endPos)");
    278.         }
    279.     }
    280. }