Hello,
I’m making an application using kinect sdk for unity provided by Microsoft.
I use specifically the BodySourceManager and my own script to track the right hand and the right hand state.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Windows.Kinect;
using System.Linq;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class DetectJoints : MonoBehaviour
{
public GameObject BodySourceManager;
public JointType TrackedJoint;
private BodySourceManager BodyManager;
private ulong TrackingId = 0;
private Body[] Bodies;
public float multi = 10f;
void Start()
{
if(BodySourceManager == null)
{
Debug.Log("Nie dodałeś");
}
else
{
BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
}
}
private void Update()
{
if (BodyManager == null)
{
return;
}
Bodies = BodyManager.GetData();
if (Bodies == null)
{
gameObject.GetComponent<Image>().enabled = false;
return;
}
foreach (var Body in Bodies)
{
if (Body.IsTracked)
{
gameObject.GetComponent<Image>().enabled = true;
TrackingId = Body.TrackingId;
//Debug.Log(TrackingId);
}
if (Body.IsTracked && TrackingId == Body.TrackingId)
{
var pos = Body.Joints[TrackedJoint].Position;
Vector2 position = new Vector2(pos.X * multi, pos.Y * multi);
Vector2 offset = new Vector2(0,0.5f);
gameObject.transform.position = new Vector2(Mathf.Clamp(position.x, 1.4f, 1.4f), Mathf.Clamp(position.y, -1.4f, 0.9f)) + new Vector2(0, -4.0f);
if(Body.HandLeftConfidence == TrackingConfidence.High)
{
switch (Body.HandRightState)
{
case HandState.Open:
gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
break;
case HandState.Closed:
gameObject.GetComponent<BoxCollider2D>().isTrigger = true;
break;
case HandState.Lasso:
gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
break;
case HandState.Unknown:
gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
break;
case HandState.NotTracked:
gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
break;
default:
gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
break;
}
}
}
}
}
}
After I get the position value from the hand I clamp the position for the ui area of the app, so the player can select one out of three options with the right hand state being closed handles the selection method by the 2d Collision.
All the time I was testing it on a normal pc but the destination for the app will be a virtual machine with passthough a gpu, usb controller and also the kinect.
I managed to make that work and also there is no input delay in the kinect studio inside the virtual machine.
So I think the culprit is my clumsy code I managed to make working (I’m very new into programing sorry).
Is my code just bad?
Thanks for your help.