Have you considered using an IEnumerator? This has been our solution to this problem.
We use the ExtendedFingerDetector to detect start/end of the gesture, and call functions to start/stop the IEnumerator.
In your case your function might look like this:;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap.Unity;
using System;
public class MoveForward : MonoBehaviour {
/// <summary>
/// A reference to the extended finger detector you are using
/// this is set in the Unity Editor.
///
/// it is not necessary to set the OnActivate and OnDeactivate in the editor, as these will be set on start
/// </summary>
public ExtendedFingerDetector detector;
/// <summary>
/// A reference to an instance of the coroutine, setup during start.
/// (If we don't use this and use the function itself, it becomes more difficult to stop
/// </summary>
private IEnumerator moveRoutine;
/// <summary>
/// Initializes the coroutine and listeners on the detector
/// </summary>
void Start () {
moveRoutine = move();
detector.OnActivate.AddListener(startMove);
detector.OnDeactivate.AddListener(stopMove);
}
/// <summary>
/// Starts the move coroutine
/// </summary>
private void startMove()
{
StartCoroutine(moveRoutine);
}
/// <summary>
/// Stops the move coroutine
/// </summary>
private void stopMove()
{
StopCoroutine(moveRoutine);
}
/// <summary>
/// Repeats each frame until stops, moves player forward.
/// </summary>
/// <returns></returns>
private IEnumerator move()
{
while (true)
{
//no need for gameobject reference here
this.transform.position += transform.forward * Time.deltaTime;
Debug.Log("MovingForward");
yield return new WaitForEndOfFrame();
}
}
}