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.