Search Unity

Touch or swipe on certain location.

Discussion in 'Scripting' started by lothlorientos, Jun 29, 2014.

  1. lothlorientos

    lothlorientos

    Joined:
    May 20, 2014
    Posts:
    32
    Hi. I'm making a 2d game in Unity with C#. I have the code for touch and swipe, but how can I make it so that something activates only when for example I swipe on a certain object?

    1. foreach (var T in Input.touches)
    2. {
    3. var P = T.position;
    4. if (T.phase == TouchPhase.Began && SwipeID == -1)
    5. {
    6. SwipeID = T.fingerId;
    7. StartPos = P;
    8. }
    9. else if (T.fingerId == SwipeID)
    10. {
    11. var delta = P - StartPos;
    12. if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
    13. {
    14. SwipeID = -1;
    15. if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
    16. {
    17. if (delta.x > 0){
    18. //code for swipe right
    19. }
    20. else if (delta.x < 0){
    21. //code for swipe left
    22. }
    23. }
    24. else
    25. {
    26. if (delta.y > 0){
    27. //code for swipe up
    28. }
    29. else if (delta.y < 0){
    30. //code for swipe down
    31. }
    32. }
    33. }
    34. else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
    35. {
    36. SwipeID = -1;
    37. //code for tap
    38. }
    39. }

    40. }