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

Need some help with this script

Discussion in 'Scripting' started by EpicAirBag, Sep 20, 2015.

  1. EpicAirBag

    EpicAirBag

    Joined:
    Sep 16, 2015
    Posts:
    7
    So I saw a video on how to use the swipe controls, and I pretty much understood everything in it, but now I need some help with something, so here is the script (I added a lot of comments to remember all the stuff,I'm still new to this).

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SwipeManager : MonoBehaviour {
    5.     private Touch iTouch = new Touch ();
    6.     private float Distance = 0;
    7.     private bool hasSwiped = false;
    8.  
    9.     void FixedUpdate() // Needed because the process is big and we need to keep track of the function at all time
    10.     {
    11.         foreach (Touch t in Input.touches)
    12.         {
    13.             if (t.phase == TouchPhase.Began) // When the user puts their finger on the screen
    14.             {
    15.                 iTouch = t;
    16.             }
    17.             else if (t.phase == TouchPhase.Moved && !hasSwiped) // When the user is moving his finger on the screen and the swipe is true
    18.             {
    19.                 float deltaX= iTouch.position.x - t.position.x;
    20.                 float deltaY= iTouch.position.y - t.position.y;
    21.                 // DistanceForm
    22.                 Distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY)); // The distance where the user started putting their finger on the screen and the final position when the user finished
    23.              
    24.                 bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
    25.              
    26.  
    27.                 if (Distance > 100f) // If the distance is more than 100 pixels than it's a swipe
    28.                 {
    29.                     if (swipedSideways && deltaX > 0) // Swiped Left (wont be used)
    30.                     {
    31.  
    32.                     }
    33.                     else if (swipedSideways && deltaX <= 0) // Swiped Right (wont be used)
    34.                     {
    35.  
    36.                     }
    37.                     else if (!swipedSideways && deltaY > 0) // Swiped Down
    38.                     {
    39.                         // function = this.
    40.                     }
    41.                     else if (!swipedSideways &&  deltaY <= 0) // Swiped up (wont be used)
    42.                     {
    43.  
    44.                     }
    45.                     hasSwiped = true; // Make the swipe false (don't excute the function all the time in the fixed update
    46.  
    47.                 }
    48.                 // Directions
    49.             }
    50.             else if (t.phase == TouchPhase.Ended) // When the user removes his finger off the screen
    51.             {
    52.                 iTouch = new Touch(); // Resets the process
    53.                 hasSwiped = false; // Resets the swiped process to make it functional
    54.             }
    55.         }
    56.     }
    57.  
    58. }
    59.  
    So, as you can probably tell I wont need the swipe to left, right, and up, I just need the swipe to down, and basically what I want it to do is when the user swipes down the first time it plays an X animation and when he swipes the second time it does Y animation, then it does X then Y etc... until it's done. Any help? xd

    Also, can I remove the other swipe controls? Like the swipe left, right and up? Is it going to affect the script if I removed them?

    I'm still new, sorry.
     
    Last edited: Sep 22, 2015
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Please check out the third "pinned" post at the top of this forum and add "Code tags" so your code is actually readable.
     
  3. EpicAirBag

    EpicAirBag

    Joined:
    Sep 16, 2015
    Posts:
    7
    Thank you.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    yes you can remove the conditionals relating to things you don't want to check for, so long as the overall if..elseif syntax is correct it'll be fine (well, it'll just be an "if..." by that point by the looks of it :p).

    You probably want to hand off the "animation control" stuff to another script at that point rather than get bogged down trying to do animation control within the swipe control script. So in the swipe control it just need to either call a function, or an event (preferable, don't need to link the two controllers together).

    The animation control could be done with a trigger within an Animator component, then just set up the FSM within the Animator to do what you need to do.
    https://unity3d.com/learn/tutorials/topics/animation (1, 2 and 4 in the controlling animation should do it)
     
  5. EpicAirBag

    EpicAirBag

    Joined:
    Sep 16, 2015
    Posts:
    7

    Alright! Thanks for the information and the tutorials! :)