多媒体互动:用Unity开发体感接物

本文介绍了一个基于Kinect v2的手势识别系统实现方案,重点介绍了如何通过挥手下指令及利用用户的左右移动来控制游戏角色以收集道具的具体实现逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:挥手或者翻页笔下开启下一页 左右移动让小人左右移动接道具,用户在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);
        }


    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值