Search Unity

Swipe on the part of the screen

Discussion in 'Scripting' started by Sweex, Apr 13, 2014.

  1. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Hi. I have 2 players in my game. I want to make separated controls for them. If I swipe left or right my player would go left or right. But I want same with my second player. So is there any way I can separate controls, for first player I should swipe on the left part of the screen and for the second, I should swipe on the right part of the screen? I already have swipe controls in my script, I only need to detect on which part of the screen player swipes. Greetings!


    Code (csharp):
    1. foreach(Touch touch in Input.touches)
    2.        {
    3.          if (touch.phase == TouchPhase.Began)
    4.          {
    5.               fp = touch.position;
    6.               lp = touch.position;
    7.          }
    8.               if (touch.phase == TouchPhase.Moved )
    9.               {
    10.                   lp = touch.position;
    11.               }
    12.               if(touch.phase == TouchPhase.Ended)
    13.               {
    14.                    if((fp.x - lp.x) > 80) // left swipe
    15.                    {
    16.  
    17.                    }
    18.                    else if((fp.x - lp.x) < -80) // right swipe
    19.                   {
    20.  
    21.                   }
    22.  
     
  2. Broller

    Broller

    Joined:
    Feb 28, 2014
    Posts:
    28
    I think you can't achieve that.
     
  3. Maximillion

    Maximillion

    Joined:
    Dec 20, 2013
    Posts:
    22
    I don't know if this will work, as i have only worked on Mobile a little... but it's just my theory.

    Divide "Screen Width" by two and store it.

    Check the position of the first two touches:

    Touch 1:
    if > "Screen Width / 2", Touch 1 = Player 2
    if < "Screen Width / 2", Touch 1 = Player 1

    Touch 2:
    if > "Screen Width / 2", Touch 2 = Player 2
    if < "Screen Width / 2", Touch 2 = Player 1

    Then carry on doing your check to see if each touch has moved 80 pixels either way from their original location.
     
    Last edited: Apr 15, 2014
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Sure you could measure where the swipe is occurring, if it is the left side, pass to player x, swipe left, right. And vice versa.
     
  5. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Maybe this photo will explain better what I want to acheieve.
     

    Attached Files:

  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    This is certainly possible. You would just need two sets of variables to keep track of player swiping actions.
    Code (csharp):
    1.  
    2. public enum SwipeDirection {
    3.     Up,
    4.     Right,
    5.     Down,
    6.     Left,
    7. }
    8.  
    9. public delegate void SwipeEventHandler(SwipeDirection direction, Vector2 anchor, Vector2 target);
    10.  
    11. public sealed class PlayerSwipe : MonoBehaviour {
    12.     public float swipeThreshold = 30;
    13.  
    14.     public bool isSwiping { get; private set; }
    15.     public Vector2 anchorPoint { get; private set; }
    16.  
    17.     // Set screen rect depending upon player.
    18.     public Rect screenArea;
    19.     // Subscribe to this event!!
    20.     public event SwipeEventHandler FingerSwiped;
    21.  
    22.     private int _fingerId;
    23.  
    24.     private void Update() {
    25.         if (isSwiping)
    26.             TrackSwiping();
    27.         else
    28.             CheckForTouchDown();
    29.     }
    30.  
    31.     private void CheckForTouchDown() {
    32.         if (isSwiping)
    33.             return;
    34.  
    35.         int touchCount = Input.touchCount;
    36.         for (int i = 0; i < touchCount; ++i) {
    37.             var touch = Input.GetTouch(i);
    38.             if (touch.phase == TouchPhase.Began  screenArea.Contains(touch.position)) {
    39.                 isSwiping = true;
    40.                 _fingerId = touch.fingerId;
    41.                 anchorPoint = touch.position;
    42.                 break;
    43.             }
    44.         }
    45.     }
    46.  
    47.     public void Cancel() {
    48.         isSwiping = false;
    49.     }
    50.  
    51.     private Touch GetInfoForTrackedTouch() {
    52.         if (!isSwiping)
    53.             return null;
    54.  
    55.         int touchCount = Input.touchCount;
    56.         for (int i = 0; i < touchCount; ++i) {
    57.             var touch = Input.GetTouch(i);
    58.             if (touch.fingerId == _fingerId)
    59.                 return touch;
    60.         }
    61.  
    62.         return null;
    63.     }
    64.  
    65.     private void TrackSwiping() {
    66.         var touch = GetInfoForTrackedTouch();
    67.         if (touch == null) {
    68.             // Info is missing for some reason, cancel swiping gesture!
    69.             Cancel();
    70.             return;
    71.         }
    72.  
    73.         if (touch.phase == TouchPhase.Moved) {
    74.             var delta = touch.position - anchorPoint;
    75.             var abs = new Vector2(Mathf.Abs(delta.x), Mathf.Abs(delta.y));
    76.  
    77.             SwipeDirection direction;
    78.  
    79.             if (abs.x >= swipeThreshold || abs.y >= swipeThreshold) {
    80.                 if (abs.x > abs.y) {
    81.                     // Horizontal swipe!
    82.                     if (delta.x > 0)
    83.                         direction = SwipeDirection.Right;
    84.                     else
    85.                         direction = SwipeDirection.Left;
    86.                 }
    87.                 else {
    88.                     // Vertical swipe!
    89.                     if (delta.y > 0)
    90.                         direction = SwipeDirection.Up;
    91.                     else
    92.                         direction = SwipeDirection.Down;
    93.                 }
    94.  
    95.                 Cancel();
    96.  
    97.                 // Invoke subscribed event handlers!
    98.                 if (FingerSwiped != null)
    99.                     FingerSwiped(direction, anchorPoint, touch.position);
    100.             }
    101.         }
    102.     }
    103. }
    104.  
    And then usage example:
    Code (csharp):
    1.  
    2. public class PlayerBehaviour : MonoBehaviour {
    3.     public int playerNumber;
    4.  
    5.     private PlayerSwipe _swipeHandler;
    6.  
    7.     private void Awake() {
    8.         _swipeHandler = GetComponent<PlayerSwipe>();
    9.         _swipeHandler.FingerSwiped += OnFingerSwiped;
    10.  
    11.         if (playerNumber == 1)
    12.             _swipeHandler.screenArea = new Rect(0, 0, Screen.width / 2, Screen.height);
    13.         else
    14.             _swipeHandler.screenArea = new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height);
    15.     }
    16.  
    17.     private void OnDisable() {
    18.         _swipeHandler.FingerSwiped -= OnFingerSwiped;
    19.     }
    20.  
    21.     private void OnFingerSwiped(SwipeDirection direction, Vector2 anchor, Vector2 target) {
    22.         // Swipe occurred for this player!
    23.     }
    24. }
    25.  
    With the above you would need to attach both of these components to your player game objects.

    Disclaimer: The above was entered directly into the forum post and has not been tested.

    I hope that this helps :)
     
    Last edited: Apr 15, 2014
  7. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Thank you for your help! I get these errors:
    PlayerSwipe.cs(140,49): error CS0037: Cannot convert null to `UnityEngine.Touch' because it is a value type
    PlayerSwipe.cs(167,32): error CS0037: Cannot convert null to `UnityEngine.Touch' because it is a value type
    PlayerSwipe.cs(182,19): error CS0019: Operator `==' cannot be applied to operands of type `UnityEngine.Touch' and `null'

    These lines are:

    Code (csharp):
    1. private Touch GetInfoForTrackedTouch() {
    2.            if (!isSwiping)
    3.         140.    return null;
    140. and 167. are the same (return null;)

    Code (csharp):
    1.  182.  if (touch == null) {        
    2.                         // Info is missing for some reason, cancel swiping gesture!
    3.                            Cancel();
    4.                        return;
    Or if it's going to be easier, in your code above these lines are 52, 61 and 66 :)
     
  8. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Sorry about that, I forgot that this was a value-type rather than a reference-type.

    Here, I have refactored the code a little for you:
    Code (csharp):
    1.  
    2. public enum SwipeDirection {
    3.     Up,
    4.     Right,
    5.     Down,
    6.     Left,
    7. }
    8.  
    9. public delegate void SwipeEventHandler(SwipeDirection direction, Vector2 anchor, Vector2 target);
    10.  
    11. public sealed class PlayerSwipe : MonoBehaviour {
    12.     public float swipeThreshold = 30;
    13.  
    14.     public bool isSwiping { get; private set; }
    15.     public Vector2 anchorPoint { get; private set; }
    16.  
    17.     // Set screen rect depending upon player.
    18.     public Rect screenArea;
    19.     // Subscribe to this event!!
    20.     public event SwipeEventHandler FingerSwiped;
    21.  
    22.     private int _fingerId;
    23.  
    24.     private void Update() {
    25.         if (Input.touchCount == 0)
    26.             return;
    27.  
    28.         bool didUpdateTrackingMotion = false;
    29.  
    30.         int touchCount = Input.touchCount;
    31.         for (int i = 0; i < touchCount; ++i) {
    32.             var touch = Input.GetTouch(i);
    33.  
    34.             if (!isSwiping) {
    35.                 if (touch.phase == TouchPhase.Began  screenArea.Contains(touch.position)) {
    36.                     CheckForTouchDown(touch);
    37.                     didUpdateTrackingMotion = true;
    38.                     break;
    39.                 }
    40.             }
    41.             else if (touch.fingerId == _fingerId) {
    42.                 TrackSwiping(touch);
    43.                 didUpdateTrackingMotion = true;
    44.                 break;
    45.             }
    46.         }
    47.  
    48.         if (isSwiping  !didUpdateTrackingMotion)
    49.             Cancel();
    50.     }
    51.  
    52.     private void CheckForTouchDown(Touch touch) {
    53.         isSwiping = true;
    54.         _fingerId = touch.fingerId;
    55.         anchorPoint = touch.position;
    56.     }
    57.  
    58.     public void Cancel() {
    59.         isSwiping = false;
    60.     }
    61.  
    62.     private void TrackSwiping(Touch touch) {
    63.         switch (touch.phase) {
    64.             case TouchPhase.Moved:
    65.                 var delta = touch.position - anchorPoint;
    66.                 var abs = new Vector2(Mathf.Abs(delta.x), Mathf.Abs(delta.y));
    67.  
    68.                 SwipeDirection direction;
    69.  
    70.                 if (abs.x >= swipeThreshold || abs.y >= swipeThreshold) {
    71.                     if (abs.x > abs.y) {
    72.                         // Horizontal swipe!
    73.                         if (delta.x > 0)
    74.                             direction = SwipeDirection.Right;
    75.                         else
    76.                             direction = SwipeDirection.Left;
    77.                     }
    78.                     else {
    79.                         // Vertical swipe!
    80.                         if (delta.y > 0)
    81.                             direction = SwipeDirection.Up;
    82.                         else
    83.                             direction = SwipeDirection.Down;
    84.                     }
    85.  
    86.                     Cancel();
    87.  
    88.                     // Invoke subscribed event handlers!
    89.                     if (FingerSwiped != null)
    90.                         FingerSwiped(direction, anchorPoint, touch.position);
    91.                 }
    92.                 break;
    93.  
    94.             case TouchPhase.Canceled:
    95.             case TouchPhase.Ended:
    96.                 Cancel();
    97.                 break;
    98.         }
    99.     }
    100. }
    101.  
    Let me know how it goes!!
     
    Last edited: Apr 15, 2014
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    You can monitor which sidet he swipe is occurring by either, checking the finger position at swipe beginning. If on left side, left player move.


    The oer is to use colliders and raycasts. The colliders, two of them, act like btns. When you finger goes down, shoot a raycast. If raycast hits btn left, you move player left (side) and vice versa.
     
  10. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    I didn't get any errors :D Thank you very much for this! Just one more question :p
    I actually have animations for left and right and basicly when player swipes left I want to play animation for left. I had that in my script and I know how to play animation with script. I'm just wondering where should I put code for playing animation ? I had this before

    Code (csharp):
    1. if((fp.x - lp.x) > 80) // left swipe
    2.  
    3.                  {
    4.                         animation.Play (animationLeft.name);
    5.                  }
    6.  
    7.  
     
    Last edited: Apr 15, 2014
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Place this in your event handler:
    Code (csharp):
    1.  
    2. private void OnFingerSwiped(SwipeDirection direction, Vector2 anchor, Vector2 target) {
    3.     switch (direction) {
    4.         case SwipeDirection.Left:
    5.             // Trigger leftward swipe animation!
    6.             break;
    7.         case SwipeDirection.Right:
    8.             // Trigger rightward swipe animation!
    9.             break;
    10.     }
    11. }
    12.  
    I fixed an error in my previous post a few minutes after I posted it. Could you just make sure that you copied the latest version.

    I placed the begin and track functionality back-to-front, so it would not have worked properly.
     
  12. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Yes, I have latest version of your code. I'm gonna try that and let you know if it's working.
     
  13. Sweex

    Sweex

    Joined:
    Jan 26, 2014
    Posts:
    25
    Thank you so so much :D It's working perfect! I'm glad we have people like you here :D
     
  14. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    No problem it was my pleasure and I am happy that it worked for you :)