Ejemplo n.º 1
0
    private void Update()
    {
        KinectAvatar avatar = KinectManager.Instance.CurrentAvatar;

        if (avatar == null)
        {
            return;
        }


        InputDevice leftController  = null;
        InputDevice rightController = null;

        if (InputManager.Devices.Count >= 2)
        { //more often than not this means player 1 in the left hand and player 2 in the right hand
            leftController  = InputManager.Devices[0];
            rightController = InputManager.Devices[1];
        }
        else if (InputManager.Devices.Count == 1)
        {
            leftController  = InputManager.Devices[0];
            rightController = InputManager.Devices[0];
        }
        else
        {
            //no controllers attached
            return;
        }

        CheckController(leftController.LeftTrigger, leftController.LeftBumper, leftController.LeftStickButton, leftController.LeftStick, 0, avatar.GetLeftHandRay());
        CheckController(rightController.RightTrigger, rightController.RightBumper, rightController.RightStickButton, rightController.RightStick, 1, avatar.GetRightHandRay());
    }
Ejemplo n.º 2
0
    protected virtual void Update()
    {
        if (Reader != null)
        {
            var frame = Reader.AcquireLatestFrame();
            if (frame != null)
            {
                if (Data == null)
                {
                    Data = new Kinect.Body[Sensor.BodyFrameSource.BodyCount];
                }

                frame.GetAndRefreshBodyData(Data);

                frame.Dispose();
                frame = null;
            }
        }

        if (Data == null)
        {
            return; //No data yet
        }
        List <ulong> trackedIds = new List <ulong>();

        foreach (Kinect.Body body in Data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                trackedIds.Add(body.TrackingId);
            }
        }

        List <ulong> knownIds = new List <ulong>(Avatars.Keys);

        // First delete untracked bodies
        foreach (ulong trackingId in knownIds)
        {
            if (trackedIds.Contains(trackingId) == false)
            {
                Avatars[trackingId].Kill();
                Avatars.Remove(trackingId);
            }
        }

        // Then add bodies / update joint positions for bodies that exist
        foreach (Kinect.Body body in Data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                if (Avatars.ContainsKey(body.TrackingId) == false)
                {
                    GameObject newAvatar = GameObject.Instantiate <GameObject>(AvatarPrefab);
                    newAvatar.transform.parent        = this.transform; //localize position to the kinect manager
                    newAvatar.transform.localPosition = Vector3.zero;
                    newAvatar.transform.localRotation = Quaternion.identity;

                    KinectAvatar avatar = newAvatar.GetComponent <KinectAvatar>();
                    avatar.Id = body.TrackingId;

                    Avatars.Add(body.TrackingId, avatar);
                }

                Avatars[body.TrackingId].UpdateBodyData(body);
            }
        }

        if (Avatars.Count > 0 && Avatars.ContainsKey(CurrentAvatarID) == false)
        {
            CurrentAvatarID = Avatars.OrderBy(avatar => avatar.Value.GetDistanceToKinect()).First().Key;
            Avatars[CurrentAvatarID].SetActiveAvatar();
        }
    }