Search Unity

Platforming movement through touch

Discussion in 'Scripting' started by Baskyn, Apr 2, 2015.

  1. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    I have my platformer script, but it runs off of GetAxis("Horizontal"), and GetButtonDown("Jump"). I'm trying to write a script that uses touch to have the movement system of LIMBO on mobile devices. If you tap or maybe swipe(depends on how they both play), you will jump. When you drag/swipe right or left no matter where your finger is on the screen, it would be the equivalent of GetAxis("Horizontal") = 1, or GetAxis("Horizontal") = -1, and when you release your finger it would be like GetAxis("Horizontal") = 1. I was going to manually change the GetAxis("Horizontal") through swipes, but found out you can't do that, so I'm at a loss now.

    I'm no novice to programming, but I'm not great at coding touch input because it confuses me. If somebody could point me somewhere that could help me do this, or explain to me how it could be done, I would be very appreciative.


    EDIT:
    Ok, I did a bit of research and figured out the "How" for the most part, I just need multitouch, and would like to jump on tap, rather than swipe up. I'm assigning the "HorizontalMovement" value to the movement, rather than GetAxis("Horizontal").
    This is what I have:
    Code (CSharp):
    1.     public void SwipeCheck()
    2.     {
    3.  
    4.         if (Input.touchCount > 0)
    5.            
    6.         {
    7.  
    8.             Touch touch = Input.touches[0];
    9.            
    10.            
    11.            
    12.             switch (touch.phase)
    13.                
    14.             {
    15.                
    16.             case TouchPhase.Began:
    17.                
    18.                 startPos = touch.position;
    19.                
    20.                 break;
    21.  
    22.             case TouchPhase.Moved:
    23.                
    24.                 float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
    25.                
    26.                 if (swipeDistVertical > minSwipeDistY)
    27.                    
    28.                 {
    29.                    
    30.                     float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
    31.                    
    32.                     if (swipeValue > 0)//up swipe
    33.  
    34.                     {
    35.  
    36.                         swipeJump = true;
    37.                     }  
    38.                         else if (swipeValue < 0)//down swipe
    39.                     {
    40.                             //Shrink ();
    41.                     }      
    42.                 }
    43.                
    44.                 float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
    45.                
    46.                 if (swipeDistHorizontal > minSwipeDistX)
    47.                    
    48.                 {
    49.                    
    50.                     float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
    51.                    
    52.                     if (swipeValue > 0)//right swipe
    53.                     {
    54.                         HorizontalMovement = 1;
    55.  
    56.                     }
    57.                         else if (swipeValue < 0)//left swipe
    58.                     {
    59.  
    60.                         HorizontalMovement = -1;
    61.                     }      
    62.                 }
    63.                 break;
    64.             }
    65.         }
    66.     }
     
    Last edited: Apr 2, 2015