Looping an Int with a Horizontal swipe

is it possible to loop through an int positively (right swipe) or negatively (left swipe) and cycle through the length of the int? I’m currently working on a mobile game that requires lanes which is an Array int of 3, which the movement is based on the target lane which equals lanes. I’m aware of how detecting a swipe works, and i know how to do it programmatically, however i do not/am not aware, of a way to loop up and down or declaring each with a left or right swipe.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobileTest : MonoBehaviour {

    private Vector3 fp;   //First touch position
    private Vector3 lp;   //Last touch position
    private float dragDistance;  //minimum distance for a swipe to be registered

    public float snapSpeed = 10f;
    public Transform[] Lanes;

    public GameObject Empty;
    public GameObject Bounce;

    private Transform targetLane;
    private Vector3 EmptyOffset = new Vector3(0, 0.2f, 0);
    private Vector3 SquashScale = new Vector3(1, 0.5f, 1);
    private Vector3 FullScale = Vector3.one;
    private int meh = 3;

    public KeyCode[] LaneInputs = new KeyCode[]
{
            KeyCode.S,
            KeyCode.D,
            KeyCode.A
};

    void Start()
    {
        dragDistance = Screen.height * 15 / 100; //dragDistance is 15% height of the screen
    }

    void Update()
    {
        int minSize = Mathf.Min(meh, Lanes.Length);

        for (int i = 0; i < minSize; i++)
        {
            if (Input.GetKeyDown(LaneInputs[i]))
            {
                targetLane = Lanes[i];
            }
        }



        if (targetLane != null)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetLane.position, snapSpeed * Time.deltaTime);
        }

        if (Input.touchCount == 1) // user is touching the screen with a single touch
        {
            Touch touch = Input.GetTouch(0); // get the touch
            if (touch.phase == TouchPhase.Began) //check for the first touch
            {
                fp = touch.position;
                lp = touch.position;
            }
            else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
            {
                lp = touch.position;
            }
            else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
            {
                lp = touch.position;  //last touch position. Ommitted if you use list

                //Check if drag distance is greater than 20% of the screen height
                if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
                {//It's a drag
                 //check if the drag is vertical or horizontal
                    if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                    {   //If the horizontal movement is greater than the vertical movement...
                        if ((lp.x > fp.x))  //If the movement was to the right)
                        {   //Right swipe
                            for (int i = 0; i < minSize; i--)
                            {
                                    targetLane = Lanes[i];
                            }
                            Debug.Log("Right Swipe");
                        }
                        else
                        {   //Left swipe
                            for (int i = 0; i < minSize; i++)
                            {
                                targetLane = Lanes[i];
                            }
                            Debug.Log("Left Swipe");
                        }
                    }
                    else
                    {   //the vertical movement is greater than the horizontal movement
                        if (lp.y > fp.y)  //If the movement was up
                        {   //Up swipe
                            Debug.Log("Up Swipe");
                        }
                        else
                        {   //Down swipe
                            Debug.Log("Down Swipe");
                        }
                    }
                }
                else
                {   //It's a tap as the drag distance is less than 20% of the screen height
                    Debug.Log("Tap");
                }
            }
        }
    }
}

i almost have an answer, however instead of cycling through all three transforms, it keeps only cycling two. Any ideas?

With regards to “looping” a value, you could use the so-called modulus (this symbol: %) operator to get the remainder of a target value from a base value. in practice:

int maxValue = 3;//base value, represents the max value which gets "looped" back to 0
int currValue = 0;

int GetLoopedValue(int value){
    return (currValue % maxValue);
}
void Update(){
    if(Input.GetKeyDown(KeyCode.A)){
        currValue--;
        print(GetLoopedValue(currValue));
    }
    if(Input.GetKeyDown(KeyCode.D)){
        currValue++;
        print(GetLoopedValue(currValue));
    }
}

The other way is simply defining a min and max, and just resetting the target value when it exceeds the min and max values.

int currValue = 0;
int minValue = 0;
int maxValue = 2;//starts counting at 0, so 3 lanes is 012

void LoopValue(int delta){
    currValue += delta;
    if(currValue > maxValue){
        currValue = minValue;
    }
    if(currValue < minValue){
        currValue = maxValue;
    }
}

With regards to only “cycling two” lanes, your script isn’t exactly cycling through the lanes as much as it is setting the target lane to be the min or max value, as you have tried to do in the two for loops between the lines 78 and 89 of your previously provided script. If you increased the number of lanes, the target lane would only be the first or last lane in your current script.

You do not have a cached int variable in your script to represent the current lane you should be on, which is what I am doing with “currValue” of my above 2 examples.

1 Like

Thank you for the response! i understand what you are getting at, and realize the problem i was having with the lanes switches only going to the min or max. Is there any way to use a cached int variable for the current in the for loop i currently have? or would you still advise the logic above?

The modulus idea kind of worked, i would get movement however after two or three movements it stopped dead in its tracks with the swipes.

The 2nd method mentioned didn’t let me move at all when swiping

It would be helpful to know what’s changed in your script after I highlighted the lane switching problem to you, and how you have tried to apply my previous examples into your own script.