Advertisement
moinularif

lastcubemencontroller

Jun 24th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4.  
  5. public class CubemanController : MonoBehaviour
  6. {
  7.     public bool MoveVertically = false;
  8.     public bool MirroredMovement = false;
  9.  
  10.     //public GameObject debugText;
  11.  
  12.     //public GameObject Hip_Center;
  13.     //public GameObject Spine;
  14.     //public GameObject Shoulder_Center;
  15.     //public GameObject Head;
  16.     //public GameObject Shoulder_Left;
  17.     //public GameObject Elbow_Left;
  18.     //public GameObject Wrist_Left;
  19.     //public GameObject Hand_Left;
  20.     //public GameObject Shoulder_Right;
  21.     //public GameObject Elbow_Right;
  22.     //public GameObject Wrist_Right;
  23.     //public GameObject Hand_Right;
  24.     //public GameObject Hip_Left;
  25.     //public GameObject Knee_Left;
  26.     //public GameObject Ankle_Left;
  27.     //public GameObject Foot_Left;
  28.     //public GameObject Hip_Right;
  29.     //public GameObject Knee_Right;
  30.     //public GameObject Ankle_Right;
  31.     //public GameObject Foot_Right;
  32.  
  33.     public LineRenderer SkeletonLine;
  34.  
  35.     //private GameObject[] bones;
  36.     private int[] parIdxs;
  37.  
  38.     private Vector3 initialPosition;
  39.     private Quaternion initialRotation;
  40.     private Vector3 initialPosOffset = Vector3.zero;
  41.     private uint initialPosUserID = 0;
  42.  
  43.     Vector3 rightWristPOS = new Vector3();
  44.     Vector3 leftWristPOS = new Vector3();
  45.     void Start()
  46.     {
  47.         //store bones in a list for easier access
  48.         //bones = new GameObject[] {
  49.         //    Hip_Center, Spine, Shoulder_Center, Head,  // 0 - 3
  50.         //    Shoulder_Left, Elbow_Left, Wrist_Left, Hand_Left,  // 4 - 7
  51.         //    Shoulder_Right, Elbow_Right, Wrist_Right, Hand_Right,  // 8 - 11
  52.         //    Hip_Left, Knee_Left, Ankle_Left, Foot_Left,  // 12 - 15
  53.         //    Hip_Right, Knee_Right, Ankle_Right, Foot_Right  // 16 - 19
  54.         //};
  55.  
  56.         parIdxs = new int[] {
  57.                         0, 0, 1, 2,
  58.                         2, 4, 5, 6,
  59.                         2, 8, 9, 10,
  60.                         0, 12, 13, 14,
  61.                         0, 16, 17, 18
  62.                 };
  63.  
  64.         initialPosition = transform.position;
  65.         initialRotation = transform.rotation;
  66.         rightWristPOS = leftWristPOS = Vector3.zero;
  67.         //transform.rotation = Quaternion.identity;
  68.     }
  69.  
  70.     // Update is called once per frame
  71.     void Update()
  72.     {
  73.         //Debug.Log("right wrist pos: "+bones[10].transform.position+" and left wrist pos: "+ bones[6].transform.position);
  74.         //rightWristPOS = bones[10].transform.position;
  75.         //leftWristPOS = bones[6].transform.position;
  76.         if (rightWristPOS.z > leftWristPOS.z)
  77.         {
  78.             Debug.Log("right turn...");
  79.         }
  80.         else
  81.         {
  82.             Debug.Log("left turn...");
  83.         }
  84.  
  85.         KinectManager manager = KinectManager.Instance;
  86.  
  87.         // get 1st player
  88.         uint playerID = manager != null ? manager.GetPlayer1ID() : 0;
  89.  
  90.         if (playerID <= 0)
  91.         {
  92.             // reset the pointman position and rotation
  93.             if (transform.position != initialPosition)
  94.             {
  95.                 transform.position = initialPosition;
  96.             }
  97.  
  98.             if (transform.rotation != initialRotation)
  99.             {
  100.                 transform.rotation = initialRotation;
  101.             }
  102.  
  103.             //for(int i = 0; i < bones.Length; i++)
  104.             //{
  105.             //    bones[i].gameObject.SetActive(true);
  106.  
  107.             //    bones[i].transform.localPosition = Vector3.zero;
  108.             //    bones[i].transform.localRotation = Quaternion.identity;
  109.             //}
  110.  
  111.             return;
  112.         }
  113.  
  114.         // set the user position in space
  115.         Vector3 posPointMan = manager.GetUserPosition(playerID);
  116.         posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;
  117.  
  118.         // store the initial position
  119.         if (initialPosUserID != playerID)
  120.         {
  121.             initialPosUserID = playerID;
  122.             initialPosOffset = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
  123.         }
  124.  
  125.         transform.position = initialPosOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
  126.  
  127.         // update the local positions of the bones
  128.         for (int i = 0; i < 20; i++) //for(int i = 0; i < bones.Length; i++)
  129.         {
  130.             //if(bones[i] != null)
  131.             //{
  132.             int joint = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i) : i;
  133.  
  134.             if (manager.IsJointTracked(playerID, joint))
  135.             {
  136.                 //bones[i].gameObject.SetActive(true);
  137.  
  138.                 Vector3 posJoint = manager.GetJointPosition(playerID, joint);
  139.                 posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;
  140.  
  141.                 Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, !MirroredMovement);
  142.                 rotJoint = initialRotation * rotJoint;
  143.  
  144.                 posJoint -= posPointMan;
  145.  
  146.                 if (MirroredMovement)
  147.                 {
  148.                     posJoint.x = -posJoint.x;
  149.                     posJoint.z = -posJoint.z;
  150.                 }
  151.  
  152.                 //bones[i].transform.localPosition = posJoint;
  153.                 if (i == 6)
  154.                 {
  155.                     leftWristPOS = posJoint;
  156.                 }
  157.                 if (i == 10)
  158.                 {
  159.                     rightWristPOS = posJoint;
  160.                 }
  161.                 //bones[i].transform.rotation = rotJoint;
  162.  
  163.             }
  164.             else
  165.             {
  166.                 //bones[i].gameObject.SetActive(false);
  167.             }
  168.             //}    
  169.         }
  170.  
  171.     }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement