需求:挥手或者翻页笔下开启下一页 左右移动让小人左右移动接道具,用户在30s内吃道具,道具总共有7种类型,每种类型吃了后 点亮一个图片。
sdk:kinectv2
挥手核心逻辑 使用KinectGestures.Gestures.SwipeDown
public class PlayerGestureListener : Singleton<PlayerGestureListener>, KinectGestures.GestureListenerInterface
{
public bool IsSwipeDown()
{
if (swipeDown)
{
swipeDown = false;
return true;
}
return false;
}
public bool swipeDown = false;
private bool progressDisplayed;
private float progressGestureTime;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (progressDisplayed && ((Time.realtimeSinceStartup - progressGestureTime) > 0.05f))
{
progressDisplayed = false;
Debug.Log("Forced progress to end.");
}
}
public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
{
if (progressDisplayed)
{
progressDisplayed = false;
}
return true;
}
public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
{
if (gesture == KinectGestures.Gestures.SwipeDown)
swipeDown = true;
return true;
}
public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
{
if ((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn) && progress > 0.5f)
{
progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
else if ((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft || gesture == KinectGestures.Gestures.LeanRight ||
gesture == KinectGestures.Gestures.LeanForward || gesture == KinectGestures.Gestures.LeanBack) && progress > 0.5f)
{
progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
else if (gesture == KinectGestures.Gestures.Run && progress > 0.5f)
{
progressDisplayed = true;
progressGestureTime = Time.realtimeSinceStartup;
}
}
public void UserDetected(long userId, int userIndex)
{
KinectManager manager = KinectManager.Instance;
manager.DetectGesture(userId, KinectGestures.Gestures.SwipeDown);
}
public void UserLost(long userId, int userIndex)
{
Debug.Log("UserLost");
}
}
左右移动的核心逻辑:
void Update()
{
KinectManager manager = KinectManager.Instance;
if (manager && manager.IsInitialized() && foregroundCamera)
{
Rect backgroundRect = foregroundCamera.pixelRect;
PortraitBackground portraitBack = PortraitBackground.Instance;
if (portraitBack && portraitBack.enabled)
{
backgroundRect = portraitBack.GetBackgroundRect();
}
int joint = (int)KinectInterop.JointType.SpineMid;
// overlay all joints in the skeleton
if (manager.IsUserDetected())
{
long userId = manager.GetUserIdByIndex(playerIndex);
if (manager.IsJointTracked(userId, joint))
{
posJoint = manager.GetJointPosColorOverlay(userId, joint, foregroundCamera, backgroundRect);
//posJoint = manager.GetJointKinectPosition(userId, joint);
//posJoint = manager.GetJointPosition(userId, joint);
//Debug.Log("GetJointPosition:" + posJoint);
//Debug.Log("转换成屏幕坐标:" + Camera.main.WorldToScreenPoint(posJoint));
//Debug.Log("GetJointPosition:" + manager.GetJointPosition(userId, joint));
//Debug.Log("GetJointKinectPosition:" + manager.GetJointKinectPosition(userId, joint));
//Debug.Log("GetJointPosColorOverlay:" + manager.GetJointPosColorOverlay(userId, joint, foregroundCamera, backgroundRect));
//Debug.Log("GetJointPosDepthOverlay:" + manager.GetJointPosDepthOverlay(userId, joint, foregroundCamera, backgroundRect));
if (posJoint != Vector3.zero)
{
Vector3 screenPoint= Camera.main.WorldToScreenPoint(posJoint);
rectTran.anchoredPosition=new Vector2(screenPoint.x, -352F);
}
}
}
}
if(Input.GetKey(KeyCode.UpArrow))
{
float x=rectTran.anchoredPosition.x;
x -= 10;
x = Mathf.Clamp(x,44,700);
rectTran.anchoredPosition = new Vector2(x, rectTran.anchoredPosition.y);
}
if (Input.GetKey(KeyCode.DownArrow))
{
float x = rectTran.anchoredPosition.x;
x += 10;
x = Mathf.Clamp(x, 44, 700);
rectTran.anchoredPosition = new Vector2(x, rectTran.anchoredPosition.y);
}
}