0
\$\begingroup\$

I'm sending events out like this in ScreenInputManager.cs, so that trees in my scene will move in the direction of a given swipe:

public delegate void SendSwipe(string swipeDirection);
public event SendSwipe moveGameItems;

private void Update()
    {
        if (Touch.activeFingers.Count == 1) {
            
            // other code here not related to this
            TriggerSwipe(startPos, endPos);
        }
    }

 private void TriggerSwipe(Vector2 start, Vector2 end)
    {
            if (Vector2.Dot(Vector2.left, direction2D) > directionThreshhold)
            {
                moveGameItems("left");
            }

            if (Vector2.Dot(Vector2.right, direction2D) > directionThreshhold)
            {
                moveGameItems("right");
            }
        }
    }

Then, in another script, I'm subscribing to those events, like this:

private ScreenInputManager inputManager;

    private void OnEnable()
    {
        inputManager.moveGameItems += MoveTree;
    }

    private void OnDisable()
    {
        inputManager.moveGameItems -= MoveTree;
    }

    private void Awake()
    {
        inputManager = ScreenInputManager.Instance;
    }

    private void MoveTree(string direction)
    {
        
        // code here
    }

Problem: However, MoveTree is called continuously - as long as the swipe continues. I think this could because TriggerSwipe() is in Update, but then again, when I Debug.Log in the function itself, it only seems to called it is actually swiped.

What I want:

I want to call MoveTree only when the screen is swiped. I have tried using boolean flags, etc., but the problem is I also want to be able to interrupt MoveTree with a swipe in the other direction if I need to.

So the input from the delegate has to be steadily received, but when it moves in a specific direction, it should move in that direction once until it has to move in another direction.

What I've tried:

I have tried boolean flags, using "yield return" in coroutines, etc., but I don't seem to be able to get it so it can be interrupted to go in the other direction if I need to.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

If the thing is that you only want the event to be called when changing directions, you could try storing the direction of the most recent movement, and comparing it to the direction of the current one. If they point in the same direction, ignore it; otherwise, call the event. Something like this comes to mind:

public delegate void SendSwipe(string swipeDirection);
public event SendSwipe moveGameItems;

Vector2 directionLastSwipe;
bool alreadyMoving;

private void Update()
{
    if (Touch.activeFingers.Count == 1) {
        // other code here not related to this
        TriggerSwipe(startPos, endPos);
        alreadyMoving = true;
    }
    else
        alreadyMoving = false;
}

private void TriggerSwipe(Vector2 start, Vector2 end)
{
    //I'm guessing direction2D is the current swipe direction
    if(alreadyMoving && Vector2.Dot(directionLastSwipe, direction2D)>0)
        return;

    if (Vector2.Dot(Vector2.left, direction2D) > directionThreshhold)
    {
        moveGameItems("left");
        directionLastSwipe = direction2D; 
    }

    if (Vector2.Dot(Vector2.right, direction2D) > directionThreshhold)
    {
        moveGameItems("right");
        directionLastSwipe = direction2D;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .