Need some help with this script

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).

using UnityEngine;
using System.Collections;

public class SwipeManager : MonoBehaviour {
    private Touch iTouch = new Touch ();
    private float Distance = 0;
    private bool hasSwiped = false;

    void FixedUpdate() // Needed because the process is big and we need to keep track of the function at all time
    {
        foreach (Touch t in Input.touches)
        {
            if (t.phase == TouchPhase.Began) // When the user puts their finger on the screen
            {
                iTouch = t;
            }
            else if (t.phase == TouchPhase.Moved && !hasSwiped) // When the user is moving his finger on the screen and the swipe is true
            {
                float deltaX= iTouch.position.x - t.position.x;
                float deltaY= iTouch.position.y - t.position.y;
                // DistanceForm
                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
             
                bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);
             

                if (Distance > 100f) // If the distance is more than 100 pixels than it's a swipe
                {
                    if (swipedSideways && deltaX > 0) // Swiped Left (wont be used)
                    {

                    }
                    else if (swipedSideways && deltaX <= 0) // Swiped Right (wont be used)
                    {

                    }
                    else if (!swipedSideways && deltaY > 0) // Swiped Down
                    {
                        // function = this.
                    }
                    else if (!swipedSideways &&  deltaY <= 0) // Swiped up (wont be used)
                    {

                    }
                    hasSwiped = true; // Make the swipe false (don't excute the function all the time in the fixed update

                }
                // Directions
            }
            else if (t.phase == TouchPhase.Ended) // When the user removes his finger off the screen
            {
                iTouch = new Touch(); // Resets the process
                hasSwiped = false; // Resets the swiped process to make it functional
            }
        }
    }

}

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.

Please check out the third “pinned” post at the top of this forum and add “Code tags” so your code is actually readable.

Thank you.

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)

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