Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with gravity and touch/swipe gestures

Discussion in 'Scripting' started by Agron, Sep 14, 2015.

  1. Agron

    Agron

    Joined:
    Mar 29, 2015
    Posts:
    7
    Hi Guys. I have problem with this code. I did not write the whole code my self.

    This script is inplemented om my player so i can move the player by swiping.

    The thing is that the player moves on all directions but if i move it out of the floor, the gravity will get very slow and player falls extremly slow. If i do not swipe and insted move the player in the editor, and outside the floor, the gravity works normal.

    Anyone that understand what my problem is and whant to help me?

    Here is the code:




    Code (CSharp):
    1. public class PlayerMovementScript : MonoBehaviour {
    2.    
    3.    
    4.     private Vector3 startPos;
    5.     private Vector3 endPos;
    6.  
    7.     private float lerpTime;
    8.     private float currentLerpTime;
    9.     private float perc = 1;
    10.     private bool firstInput;
    11.  
    12.     private Touch initialTouch = new Touch();
    13.     private float distance = 0;
    14.     private bool hasSwiped = false;
    15.  
    16.  
    17.  
    18.  
    19.  
    20.     void FixedUpdate()
    21.     {
    22.        
    23.  
    24.         foreach (Touch t in Input.touches)
    25.         {
    26.             if (t.phase == TouchPhase.Began)
    27.             {
    28.                 initialTouch = t;
    29.             }
    30.             else if (t.phase == TouchPhase.Moved && !hasSwiped)
    31.             {
    32.                 float deltaX = initialTouch.position.x - t.position.x;
    33.                 float deltaY = initialTouch.position.y - t.position.y;
    34.                 distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
    35.                 bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
    36.  
    37.                 if (distance > 10f)
    38.                 {
    39.                     if (swipedSideways && deltaX > 0 || swipedSideways && deltaX <= 0 || !swipedSideways && deltaY > 0 || !swipedSideways && deltaY <= 0)
    40.                     {
    41.                         if (perc == 1)
    42.                         {
    43.                             lerpTime = 1;
    44.                             currentLerpTime = 0;
    45.                             firstInput = true;
    46.                            
    47.                         }
    48.                     }
    49.                     startPos = gameObject.transform.position;
    50.  
    51.                     if (swipedSideways && deltaX > 0) //swiped left
    52.                     {
    53.                          endPos = new Vector3(transform.position.x + 2, transform.position.y, transform.position.z);
    54.  
    55.                     }
    56.                     else if (swipedSideways && deltaX <= 0) //swiped right
    57.                     {
    58.  
    59.                        endPos = new Vector3(transform.position.x - 2, transform.position.y, transform.position.z);
    60.                     }
    61.                     else if (!swipedSideways && deltaY > 0) //swiped down
    62.                     {
    63.                         endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 2);
    64.  
    65.                     }
    66.                     else if (!swipedSideways && deltaY <= 0)  //swiped up
    67.                     {
    68.                        endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 2);
    69.  
    70.  
    71.                     }
    72.  
    73.                     hasSwiped = true;
    74.                 }
    75.  
    76.             }
    77.             else if (t.phase == TouchPhase.Ended)
    78.             {
    79.                 initialTouch = new Touch();
    80.                 hasSwiped = false;
    81.             }
    82.         }
    83.  
    84.  
    85.         if (firstInput == true)
    86.         {
    87.             currentLerpTime += Time.deltaTime * 17f;
    88.             perc = currentLerpTime / lerpTime;
    89.             gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
    90.             if (perc > 0.8f)
    91.             {
    92.                 perc = 1;
    93.             }
    94.            
    95.         }
    96.     }
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    103.  
    104.  
    105.    
    106. }
    107.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    It does not appear that you are ever clearing firstInput. Is this by design?

    If there were no touches going on and firstInput was still true, wouldn't that interfere with your falling?
     
  3. Agron

    Agron

    Joined:
    Mar 29, 2015
    Posts:
    7
    No without touches going on My falling is normal, after first touch it falls slow and dont detect collisions :/
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not used touch detection much myself... but are you supposed to be detecting them in fixed update? surely that'll run into the same issues other input detection have where the fixedupdate isn't running every frame so they are missed.

    Also, having just said that, it doesn't appear that you do anything involving physics in your code, so why are you using fixedupdate at all?


    oh and you're directly modifying the position of the object, it's not going to be affected by collision then since it's not moving via the physics engine.
     
  5. Agron

    Agron

    Joined:
    Mar 29, 2015
    Posts:
    7

    So what do you suggest?

    Thanks for the answer.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Sorry, you are absolutely correct! If you try to detect touches in the FixedUpdate, you will miss some of them.

    What you must do is detect the touches in Update() and record the inputs, then in FixedUpdate() apply them.

    This goes along nicely with encapsulating the input portion of your code apart and away from the action part of your code (which you should strive to do in any case) such that it can be taken from a variety of sources, such as AI, the network, the keyboard, etc.
     
  7. Agron

    Agron

    Joined:
    Mar 29, 2015
    Posts:
    7
    I reacreated the whole code from beginning and solved the problem. Thank you for your help guys.