Unity 上下挥动算法(kinect 作揖动作识别)

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

public class Test : MonoBehaviour {
    public int count = 2;
    int currentIndex;
    float lastPosY;
    float currenPosY;
    bool isSwipeUp = true;
    float offset =100f;
	void Start () {
		
	}
	
	void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            lastPosY = Input.mousePosition.y;
            isSwipeUp = true;
            currentIndex = 0;
        }
        if (Input.GetMouseButton(0)) 
        {
            currenPosY = Input.mousePosition.y;
            if(currentIndex < count) // 控制上下挥动的次数
            {
                if (isSwipeUp)
                {
                    if ((currenPosY - lastPosY) > offset)
                    {
                        lastPosY = currenPosY;
                        Debug.Log("SwipeUP");
                        isSwipeUp = !isSwipeUp;
                    }
                }
                else
                {
                    if ((currenPosY - lastPosY) < -offset)
                    {
                        lastPosY = currenPosY;
                        isSwipeUp = !isSwipeUp;
                        Debug.Log("SwipeDown");
                        currentIndex++;
                        Debug.Log(currentIndex);
                        if(currentIndex.ToString() == count.ToString()) // 整数相等判断
                        {
                            DoSomething();
                        }

                    }
                }
            }


        }
    }

    /// 
    /// 次数达到,执行此方法
    /// 
    void DoSomething()
    {
        // to do something
        Debug.Log("你已经上下挥动达到了次数!");
    }
}

测试结果如图所示:

Unity 上下挥动算法(kinect 作揖动作识别)_第1张图片

你可能感兴趣的:(unity,Kinect)