VR开发实战HTC Vive项目之医院实验室

一、框架视图

二、主要代码

注:本文中的通讯协议为虚拟协议
Hand

Hand_Indexfinger


using System.Threading;
using UnityEngine;

public class Hand_Indexfinger : MonoBehaviour
{

    public GameObject indexfinger;

    private Quaternion _indexfinger;//食指旋转角度
    private float indexfingerZ;//食指
    private float indexfingerZchild;
    private float tempindexfingerZ;



    //食指锁定协议
    public byte byteIndexfinger_H;
    public byte byteIndexfinger_L;


    public  bool isTouch_indexfinger; //是否触碰到
    public  float elasticIndexfinger;//食指的弹性变化度
    public  float elasticIndexfingerTemp;//食指的弹性变化度
    public  byte[] byteIndexfinger; //存贮食指高字节和低字节


  // public  float timer=0; //定义定时器


    void Awake()
    {

        _indexfinger = indexfinger.transform.rotation;

        //食指
        indexfingerZ = indexfinger.transform.localEulerAngles.z;
        indexfingerZchild = indexfinger.transform.GetChild(0).GetChild(0).localEulerAngles.z;
        tempindexfingerZ = indexfingerZchild;

    }

    void Start()
    {
      //  HandManager.instance.timer = HandManager.instance.HandManager.instance.timer;

    }
    void FixedUpdate()
    {

        Ctrl_indexfinger();
        if (isTouch_indexfinger)
        {

            //HandManager.instance.timer += Time.deltaTime;
            if (HandManager.instance.timer >= 0f && HandManager.instance.timer < 1f)
            {
                //elasticIndexfinger= elasticIndexfingerTemp+30;
                elasticIndexfinger += 2;
                Debug.Log("食指0.5秒++elasticIndexfinger-----------" + elasticIndexfinger);

            }

            if (HandManager.instance.timer >= 1f)
            {
                // elasticIndexfinger = elasticIndexfingerTemp;
                // elasticIndexfinger -= 200;
                //elasticIndexfinger = elasticIndexfingerTemp -30;
                elasticIndexfinger -= -2;

                Debug.Log("食指1秒++elasticIndexfinger-----------" + elasticIndexfinger);
            }

            //if (HandManager.instance.timer>=1f)
            //{
            //    HandManager.instance.timer = 0;
            //    Debug.Log("食指时间归零"); 
            //}


            byteIndexfinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticIndexfinger);
            byteIndexfinger_H = byteIndexfinger[0];
            byteIndexfinger_L = byteIndexfinger[1];
        }
        else
        {
            //  byteIndexfinger_H = 0X00;
            //  byteIndexfinger_L = 0X00;
        }


    }



    /// <summary>
    /// 进入触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerEnter(Collider coll)
    {

        //if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name !=null )
        {

            if (coll.gameObject.tag=="Heart")
            {
                elasticIndexfinger = HandManager.instance.handSocket.angleX_indexfinger;
                elasticIndexfingerTemp = elasticIndexfinger;
                isTouch_indexfinger = true;
            }
            else
            {
                 byteIndexfinger_H = HandManager.instance.handSocket.recvDataTemp[20];
                 byteIndexfinger_L = HandManager.instance.handSocket.recvDataTemp[21];
            }




        }

    }





    /// <summary>
    /// 离开触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerExit(Collider coll)
    {

        //if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name != null)
        {
            byteIndexfinger_H = 0X00;
            byteIndexfinger_L = 0X00;

            isTouch_indexfinger = false;
        }

    }


    /// <summary>
    /// 退出时 复位
    /// </summary>
    void OnApplicationQuit()
    {

        indexfinger.transform.rotation = _indexfinger;


    }

    /// <summary>
    /// 食指
    /// </summary>
    private void Ctrl_indexfinger()
    {

        // indexfinger.transform.localEulerAngles = new Vector3(indexfinger.transform.localEulerAngles.x, Map(-angleY_indexfinger, 693, 769, -48, 0)+120, -MapData(angleX_indexfinger, 220, 900, -90.137f,5.733f)-60);
        indexfinger.transform.localEulerAngles = new Vector3(indexfinger.transform.localEulerAngles.x, indexfinger.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_indexfinger, 220, 900, -90.137f, 5.733f) - 60);
        if (indexfinger.transform.localEulerAngles.z - 600 < indexfingerZ)
        {
            tempindexfingerZ = -MapData(HandManager.instance.handSocket.angleX_indexfinger, 220, 900, -90.137f, 5.733f) - 60;
        }
        else
        {
            tempindexfingerZ = indexfingerZchild;
        }
        indexfinger.transform.GetChild(0).localEulerAngles = new Vector3(indexfinger.transform.GetChild(0).localEulerAngles.x, indexfinger.transform.GetChild(0).localEulerAngles.y, tempindexfingerZ - 25);
        indexfinger.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(indexfinger.transform.GetChild(0).localEulerAngles.x, indexfinger.transform.GetChild(0).localEulerAngles.y, tempindexfingerZ - 15);
    }

    /// <summary>
    /// 获取两数之间对应的取值范围
    /// </summary>
    /// <param name="value"></param>
    /// <param name="min_in"></param>
    /// <param name="max_in"></param>
    /// <param name="min_out"></param>
    /// <param name="max_out"></param>
    /// <returns></returns>
    public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
    {

        if (min_in > max_in)
        {
            min_in = min_in + max_in;
            max_in = min_in - max_in;
            min_in = min_in - max_in;

            min_out = min_out + max_out;
            max_out = min_out - max_out;
            min_out = min_out - max_out;

        }

        if (value < min_in)
            value = min_in;
        if (value > max_in)
            value = max_in;

        return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
    }
}

Hand_Midfiger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class Hand_Midfiger : MonoBehaviour {

    public GameObject midfinger;

    private Quaternion _midfinger;


    private float midfingerZ;//中指
    private float midfingerZchild;
    private float tempmidfingerZ;

    //中指锁定协议
    public byte byteMidfinger_H;
    public byte byteMidfinger_L;

    public bool isTouch_midfinger; //是否触碰到中指
    public float elasticMidfinger;//中指弹性变化度
    public float elasticMidfingerTemp;//中指弹性变化度
    public byte[] byteMinfinger;    //存贮中指的高字节和低字节

   // public float timer=0; //计时器

    void Awake() {

        _midfinger = midfinger.transform.rotation; //初始赋值

        //中指
        midfingerZ = midfinger.transform.localEulerAngles.z;
        midfingerZchild = midfinger.transform.GetChild(0).localEulerAngles.z;
        tempmidfingerZ = midfingerZchild;

    }

    void Start() {
      //  HandManager.instance.timer =HandManager.instance.timer;
    }


    void FixedUpdate() {

        Ctrl_midfinger(); //获取手指状态信息

        if (isTouch_midfinger)
        {
          // HandManager.instance.timer += Time.deltaTime;

            if (HandManager.instance.timer >= 0f&&HandManager.instance.timer<1f)
                {
                    //elasticMidfinger = elasticMidfingerTemp + 30;
                    elasticMidfinger += 2;
                Debug.Log("0.5f中指::"+elasticMidfinger);
            }

            if (HandManager.instance.timer>=1f&&HandManager.instance.timer<2f)
            {
                //elasticMidfinger = elasticMidfingerTemp - 30;
                elasticMidfinger -= 2;
                Debug.Log("1f中指::"+elasticMidfinger);
            }


            byteMinfinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticMidfinger);
            byteMidfinger_H = byteMinfinger[0];
            byteMidfinger_L = byteMinfinger[1];
        }
        else
        {
           // byteMidfinger_H = 0X00;
           // byteMidfinger_L = 0X00;
        }

    }


    void OnApplicationQuit()
    {

        midfinger.transform.rotation = _midfinger;
    }




    /// <summary>
    /// 进入触发器

    void OnTriggerEnter(Collider coll)
    {

        //if (coll.gameObject.name == Tags.Sphere)
        //if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name !=null )
        {

            if (coll.gameObject.tag=="Heart")
            {
                isTouch_midfinger = true;
                elasticMidfinger = HandManager.instance.handSocket.angleX_midfinger;
                elasticMidfingerTemp = elasticMidfinger;
            }
            else
            {
                byteMidfinger_H = HandManager.instance.handSocket.recvDataTemp[14];
                byteMidfinger_L = HandManager.instance.handSocket.recvDataTemp[15];
            }

        }

    }

    /// <summary>
    /// 离开触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerExit(Collider coll)
    {

        if (coll.gameObject.name != null)
        {
            byteMidfinger_H = 0X00;
            byteMidfinger_L = 0X00;
            isTouch_midfinger = false;
        }

    }


    /// <summary>
    /// 中指状态
    /// </summary>

    private void Ctrl_midfinger()
    {

        midfinger.transform.localEulerAngles = new Vector3(midfinger.transform.localEulerAngles.x, midfinger.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_midfinger, 260, 900, -90.137f, 5.733f) - 85);
        if (midfinger.transform.localEulerAngles.z - 600 < midfingerZ)
        {
           // tempmidfingerZ = -MapData(HandManager.instance.handSocket.angleX_midfinger, 260, 900, -90.137f, 5.733f) - 85;
            tempmidfingerZ = -MapData(HandManager.instance.handSocket.angleX_midfinger, 220, 900, -90.137f, 5.733f) - 60;
        }
        else
        {
            tempmidfingerZ = midfingerZchild;
        }
        midfinger.transform.GetChild(0).localEulerAngles = new Vector3(midfinger.transform.GetChild(0).localEulerAngles.x, midfinger.transform.GetChild(0).localEulerAngles.y, tempmidfingerZ - 25);
        midfinger.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(midfinger.transform.GetChild(0).localEulerAngles.x, midfinger.transform.GetChild(0).localEulerAngles.y, tempmidfingerZ - 15);

    }

    /// <summary>
    /// 获取两数间的取值范围
    /// </summary>

    public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
    {

        if (min_in > max_in)
        {
            min_in = min_in + max_in;
            max_in = min_in - max_in;
            min_in = min_in - max_in;

            min_out = min_out + max_out;
            max_out = min_out - max_out;
            min_out = min_out - max_out;

        }

        if (value < min_in)
            value = min_in;
        if (value > max_in)
            value = max_in;

        return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
    }

}

Hand_Pinkie

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class Hand_Pinkie : MonoBehaviour
{

    public GameObject pinkie;
    private Quaternion _pinkie;
    private float pinkieZ; //尾指
    private float pinkieZchild;
    private float temppinkieZ;
    //锁定协议
    public byte bytePinkie_H;
    public byte bytePinkie_L;

    public bool isTouch_pinkie; //是否触碰到尾指
    public float elasticPinkie;//尾指弹性变化度
    public float elasticPinkieTemp; //尾指弹性变化度
    public byte[] bytePinkie;//存贮尾指的高字节和低字节

    //public float timer = 0;//计时器

    void Awake()
    {
        _pinkie = pinkie.transform.rotation;

        //尾指
        pinkieZ = pinkie.transform.localEulerAngles.z;
        pinkieZchild = pinkie.transform.GetChild(0).localEulerAngles.z;
        temppinkieZ = pinkieZchild;

    }

    void Start()
    {

      //  HandManager.instance.timer = HandManager.instance.timer;
    }


    void FixedUpdate()
    {

        Ctrl_pinkie();

        if (isTouch_pinkie)
        {
          //  HandManager.instance.timer += Time.deltaTime;
            if (HandManager.instance.timer >= 0 && HandManager.instance.timer <1f)
            {
                //elasticPinkie = elasticPinkieTemp + 30;
                elasticPinkie +=2;
                Debug.Log("0.5f尾指::" + elasticPinkie);
            }

            if (HandManager.instance.timer >= 2f && HandManager.instance.timer < 2f)
            {
                //elasticPinkie = elasticPinkie - 30;
                elasticPinkie -=2;
                Debug.Log("1f尾指::" + elasticPinkie);
            }


            bytePinkie = HandManager.instance.handSocket.GetHightByteLowByte(elasticPinkie);
            bytePinkie_H = bytePinkie[0];
            bytePinkie_L = bytePinkie[1];

        }

    }





    /// <summary>
    /// 进入触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerEnter(Collider coll)
    {
        //if (coll.gameObject.name == Tags.Sphere)
        // if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name != null)
        {

            if (coll.gameObject.tag == "Heart")
            {
                isTouch_pinkie = true;
                elasticPinkie = HandManager.instance.handSocket.angleX_pinkie;
                elasticPinkieTemp = elasticPinkie;

            }
            else
            {

                bytePinkie_H = HandManager.instance.handSocket.recvDataTemp[2];
                bytePinkie_L = HandManager.instance.handSocket.recvDataTemp[3];
                 
            }

        }
    }

    /// <summary>
    /// 离开触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerExit(Collider coll)
    {

        if (coll.gameObject.name != null)
        {
            bytePinkie_H = 0X00;
            bytePinkie_L = 0X00;
            isTouch_pinkie = false;
        }

    }

    void OnApplicationQuit()
    {

        pinkie.transform.rotation = _pinkie;

    }


    /// <summary>
    /// 尾指
    /// </summary>
    private void Ctrl_pinkie()
    {

        pinkie.transform.localEulerAngles = new Vector3(pinkie.transform.localEulerAngles.x, pinkie.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_pinkie, 260, 900, -90.137f, 5.733f) - 85);
        if (pinkie.transform.localEulerAngles.z - 600 < pinkieZ)
        {
            temppinkieZ = -MapData(HandManager.instance.handSocket.angleX_pinkie, 260, 900, -90.137f, 5.733f) - 60;
            //tempringfingerZ = -MapData(HandManager.instance.handSocket.angleX_ringfinger, 260, 900, -90.137f, 5.733f) - 85;
        }
        else
        {
            temppinkieZ = pinkieZchild;
        }
        pinkie.transform.GetChild(0).localEulerAngles = new Vector3(pinkie.transform.GetChild(0).localEulerAngles.x, pinkie.transform.GetChild(0).localEulerAngles.y, temppinkieZ - 25);
        pinkie.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(pinkie.transform.GetChild(0).localEulerAngles.x, pinkie.transform.GetChild(0).localEulerAngles.y, temppinkieZ - 15);



    }

    public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
    {

        if (min_in > max_in)
        {
            min_in = min_in + max_in;
            max_in = min_in - max_in;
            min_in = min_in - max_in;

            min_out = min_out + max_out;
            max_out = min_out - max_out;
            min_out = min_out - max_out;

        }

        if (value < min_in)
            value = min_in;
        if (value > max_in)
            value = max_in;

        return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
    }
}


Hand_Ringfiger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class Hand_Ringfiger : MonoBehaviour
{

    public GameObject ringfinger; //持有无名指的引用

    private Quaternion _ringfinger; //记录原始角度

    private float ringfingerZ; //无名指
    private float ringfingerZchild;
    private float tempringfingerZ;


    //无名指锁定协议
    public byte byteRingfinger_H;
    public byte byteRingfinger_L;


    public bool isTouch_ringfinger; //是否触碰到无名指
    public float elasticRingfinger;//无名指弹性变化度
    public float elasticRingfingerTemp;//无名指弹性变化度
    public byte[] byteRingfinger;//存贮无名指的高字节和低字节 

    //public float timer = 0;//计时器

    void Awake()
    {

        _ringfinger = ringfinger.transform.rotation;

        //无名指
        ringfingerZ = ringfinger.transform.localEulerAngles.z;
        ringfingerZchild = ringfinger.transform.GetChild(0).localEulerAngles.z;
        tempringfingerZ = ringfingerZchild;

    }

    void Start()
    {

    // HandManager.instance.timer = HandManager.instance.timer;
    }

    void FixedUpdate()
    {

        Ctrl_ringfinger(); //检测手指状态

        if (isTouch_ringfinger)
        {
           // HandManager.instance.timer += Time.deltaTime;
            if (HandManager.instance.timer >= 0f&&HandManager.instance.timer<1f)
                {
                    //elasticRingfinger = elasticRingfingerTemp + 30;
                    elasticRingfinger +=2;
                Debug.Log("0.5f无名指::" + elasticRingfinger);
            }

            if (HandManager.instance.timer >= 1f && HandManager.instance.timer < 2f)
            {
                //elasticRingfinger = elasticRingfingerTemp - 30;
                elasticRingfinger -=2;
                Debug.Log("1.0f无名指::" + elasticRingfinger);
            }

            byteRingfinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticRingfinger);
            byteRingfinger_H = byteRingfinger[0];
            byteRingfinger_L = byteRingfinger[1];

        }
        else
        {
          //  byteRingfinger_H = 0X00;
           // byteRingfinger_L = 0X00;
        }


    }



    /// <summary>
    /// 进入触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerEnter(Collider coll)
    {
        //if (coll.gameObject.name == Tags.Sphere)
        //if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name != null)
        {

            if (coll.gameObject.tag == "Heart")
            {
                isTouch_ringfinger = true;
                elasticRingfinger = HandManager.instance.handSocket.angleX_ringfinger;
                elasticRingfingerTemp = elasticRingfinger;

            }
            else
            {
                byteRingfinger_H = HandManager.instance.handSocket.recvDataTemp[8];
                byteRingfinger_L = HandManager.instance.handSocket.recvDataTemp[9];

            }

        }
    }

    /// <summary>
    /// 离开触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerExit(Collider coll)
    {
        //  if (coll.gameObject.name == Tags.Sphere)
        // if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name != null)
        {
            byteRingfinger_H = 0X00;
            byteRingfinger_L = 0X00;
            isTouch_ringfinger = false;
        }

    }


    void OnApplicationQuit()
    {
        ringfinger.transform.rotation = _ringfinger;
    }


    /// <summary>
    /// 无名指
    /// </summary>
    private void Ctrl_ringfinger()
    {

        ringfinger.transform.localEulerAngles = new Vector3(ringfinger.transform.localEulerAngles.x, ringfinger.transform.localEulerAngles.y, -MapData(HandManager.instance.handSocket.angleX_ringfinger, 260, 900, -90.137f, 5.733f) - 60);
        if (ringfinger.transform.localEulerAngles.z - 600 < ringfingerZ)
        {
            //tempringfingerZ = -MapData(HandManager.instance.handSocket.angleX_ringfinger, 260, 900, -90.137f, 5.733f) - 85;
            tempringfingerZ = -MapData(HandManager.instance.handSocket.angleX_ringfinger, 220, 900, -90.137f, 5.733f) - 60;
        }
        else
        {
            tempringfingerZ = ringfingerZchild;
        }
        ringfinger.transform.GetChild(0).localEulerAngles = new Vector3(ringfinger.transform.GetChild(0).localEulerAngles.x, ringfinger.transform.GetChild(0).localEulerAngles.y, tempringfingerZ - 25);
        ringfinger.transform.GetChild(0).GetChild(0).localEulerAngles = new Vector3(ringfinger.transform.GetChild(0).localEulerAngles.x, ringfinger.transform.GetChild(0).localEulerAngles.y, tempringfingerZ - 15);


    }

    /// <summary>
    /// 转换值
    /// </summary>

    public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
    {

        if (min_in > max_in)
        {
            min_in = min_in + max_in;
            max_in = min_in - max_in;
            min_in = min_in - max_in;

            min_out = min_out + max_out;
            max_out = min_out - max_out;
            min_out = min_out - max_out;

        }

        if (value < min_in)
            value = min_in;
        if (value > max_in)
            value = max_in;

        return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
    }

}


Hand_Thumb

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class Hand_Thumb : MonoBehaviour
{

    public GameObject thumb;  //持有拇指的引用

    //记录旋转角度
    private Quaternion _thumb;

    private float thumbZ; //拇指
    private float thumbZchild;  //拇指的子物体
    private float tempthumbZ; //存放值

    //拇指锁定协议
    public byte byteThumb_H;
    public byte byteThumb_L;

    public bool isTouch_thumb; //是否触碰到拇指
    public float elasticThumb;//拇指的弹性变化度
    public float elasticThumbTemp;//拇指的弹性变化度
    public byte[] byteThumb; //存贮拇指的高字节和低字节

    //public float timer=0; //计时器



    public bool isExit;

    void Awake()
    {

        _thumb = thumb.transform.rotation;

        thumbZ = thumb.transform.localEulerAngles.z;
        thumbZchild = thumb.transform.GetChild(0).GetChild(0).localEulerAngles.z;
        tempthumbZ = thumbZchild;

    }


    /// <summary>
    /// 物理检测手指
    /// </summary>
    void FixedUpdate()
    {

        Ctrl_thumb();

        if (isTouch_thumb)
        {
            // HandManager.instance.timer += Time.deltaTime;
            if (HandManager.instance.timer >= 0f && HandManager.instance.timer < 1f)
            {
                //elasticThumb = elasticThumbTemp + 50;
                elasticThumb += 2;
                Debug.Log("0.5f拇指" + elasticThumb);
            }

            if (HandManager.instance.timer >= 1f && HandManager.instance.timer <2f)
            {
                //elasticThumb = elasticThumbTemp - 50;
                elasticThumb -= 2;
                Debug.Log("1f拇指:" + elasticThumb);
            }


            byteThumb = HandManager.instance.handSocket.GetHightByteLowByte(elasticThumb);
            byteThumb_H = byteThumb[0];
            byteThumb_L = byteThumb[1];

        }
        else
        {
            // byteThumb_H = 0X00;
            // byteThumb_L = 0X00;

        }


    }


    /// <summary>
    /// 进入触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerEnter(Collider coll)
    {

        if (coll.gameObject.name != null)
        {

            if (coll.gameObject.tag == "Heart")
            {
                isTouch_thumb = true;

                elasticThumb = HandManager.instance.handSocket.angleX_thumb;
                elasticThumbTemp = elasticThumb;
            }
            else
            {
                byteThumb_H = HandManager.instance.handSocket.recvDataTemp[26];
                byteThumb_L = HandManager.instance.handSocket.recvDataTemp[27];
            }


            if (coll.gameObject.name == "R_thumb_end")
            {
                isExit = false;
            }

        }
    }

    /// <summary>
    /// 离开触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerExit(Collider coll)
    {

        if (coll.gameObject.name != null)
        {
            byteThumb_H = 0X00;
            byteThumb_L = 0X00;
            isTouch_thumb = false;
        }

        if (coll.gameObject.name == "R_thumb_end")
        {
            isExit = true;
        }

    }

    void OnApplicationQuit()
    {

        thumb.transform.rotation = _thumb;

    }
    /// <summary>
    /// 拇指
    /// </summary>
    private void Ctrl_thumb()
    {

        //thumb.transform.localEulerAngles = new Vector3(thumb.transform.localEulerAngles.x, Map(angleY_thumb, 693, 769, -35, 40), MapData(angleX_thumb, 400, 730, -26.361f, -115.18f));
        thumb.transform.localEulerAngles = new Vector3(thumb.transform.localEulerAngles.x, thumb.transform.localEulerAngles.y, MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f) + 25);
        if (thumb.transform.localEulerAngles.z - 800 < thumbZ)
        {

            tempthumbZ = MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f);
        }
        else
        {
            tempthumbZ = thumbZchild;
        }
        thumb.transform.GetChild(0).localEulerAngles = new Vector3(thumb.transform.GetChild(0).localEulerAngles.x, thumb.transform.GetChild(0).localEulerAngles.y, tempthumbZ + 45);

        // str = "Y轴:   " + MapData(angleY_thumb, 693, 769, -35, 40) + "       Z轴::" + MapData(angleX_thumb, 400, 730, -26.361f, -115.18f);



    }



    public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
    {

        if (min_in > max_in)
        {
            min_in = min_in + max_in;
            max_in = min_in - max_in;
            min_in = min_in - max_in;

            min_out = min_out + max_out;
            max_out = min_out - max_out;
            min_out = min_out - max_out;

        }

        if (value < min_in)
            value = min_in;
        if (value > max_in)
            value = max_in;

        return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
    }
}

HandManager

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class HandManager : MonoBehaviour
{

    //SkinnedMesh 播放动画时与MeshCollider不同步问题解决方案
    public SkinnedMeshRenderer meshRenderer;
    public MeshCollider meshCollider;


    public static HandManager instance;//单例模式


    [HideInInspector]
    public HandSocket handSocket;

    [HideInInspector]
    public Hand_Thumb hand_Thumb;
    [HideInInspector]
    public Hand_Indexfinger hand_Indexfinger;
    [HideInInspector]
    public Hand_Midfiger hand_Midfiger;
    [HideInInspector]
    public Hand_Ringfiger hand_Ringfiger;
    [HideInInspector]
    public Hand_Pinkie hand_Pinkie;



    // private Thread connectThread;

    public float timer = 0; //计时器

    public bool isTouch; //是否触碰指定物体


    void Awake()
    {
        instance = this;
        handSocket = FindObjectOfType<HandSocket>();

        //获取持有的脚本
        hand_Thumb = FindObjectOfType<Hand_Thumb>();
        hand_Indexfinger = FindObjectOfType<Hand_Indexfinger>();
        hand_Midfiger = FindObjectOfType<Hand_Midfiger>();
        hand_Ringfiger = FindObjectOfType<Hand_Ringfiger>();
        hand_Pinkie = FindObjectOfType<Hand_Pinkie>();

        //connectThread = new Thread(new ThreadStart(SocketSendToHand));
        //connectThread.IsBackground = true;
        //connectThread.Start();
    }

    void Start()
    {

        // print("整型::"+a+"高字节:::"+GetHightWord(a)+"低字节::"+GetLowWord(a));
        //byte[] bs2 = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)888));// 注意0x1234
        //print("字节:::"+(bs2[0].ToString("X2") + " " + bs2[1].ToString("X2")));
    }

    //更新皮肤渲染跟网络碰撞器的同步
    void Update()
    {

        Mesh collideMesh = new Mesh();
        meshRenderer.BakeMesh(collideMesh); //更新mesh
        meshCollider.sharedMesh = null;
        meshCollider.sharedMesh = collideMesh; //将新的mesh赋值给meshcollider

        if (isTouch)
        {
            timer += Time.deltaTime;
            if (timer>=2f)
            {
                timer = 0;
            }
            Debug.Log(timer);
        }

    }

    //更新发送数据给手柄
    void FixedUpdate()
    {
        SocketSendToHand();

    }


    /// <summary>
    /// 发送数据给手柄
    /// </summary>
    void SocketSendToHand()
    {
            //正向锁定协议
            handSocket.handBytesFore[2] = hand_Pinkie.bytePinkie_H;
            handSocket.handBytesFore[3] = hand_Pinkie.bytePinkie_L;
            handSocket.handBytesFore[4] = hand_Ringfiger.byteRingfinger_H;
            handSocket.handBytesFore[5] = hand_Ringfiger.byteRingfinger_L;
            handSocket.handBytesFore[6] = hand_Midfiger.byteMidfinger_H;
            handSocket.handBytesFore[7] = hand_Midfiger.byteMidfinger_L;
            handSocket.handBytesFore[8] = hand_Indexfinger.byteIndexfinger_H;
            handSocket.handBytesFore[9] = hand_Indexfinger.byteIndexfinger_L;
            handSocket.handBytesFore[10] = hand_Thumb.byteThumb_H;
            handSocket.handBytesFore[11] = hand_Thumb.byteThumb_L;
            // Debug.Log("触碰到的字节数::::::" + handSocket.ByteToHexStr(handSocket.handBytesFore));

            handSocket.SocketSendTo(handSocket.handBytesFore);
           
        

    }


}

HandSocket

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HandSocket : HandUdpClient {

    //手柄触碰协议
    public byte[] handBytesColl;

    //手柄正向锁定协议
    public byte[] handBytesFore = new byte[15];  //定义好长度

    //手柄释放电机
    public byte[] handBytesRelax;


    void Awake() {

        //定义 初始化字节数  触碰协议
        handBytesColl = new byte[10];
        handBytesColl[0] = 0XED;
        handBytesColl[1] = 0XED;
        handBytesColl[2] = 0X00;
        handBytesColl[3] = 0X00;
        handBytesColl[4] = 0X00;
        handBytesColl[5] = 0X00;
        handBytesColl[6] = 0X00;
        handBytesColl[7] = 0X00;
        handBytesColl[8] = 0XFF;
        handBytesColl[9] = 0XFF;

       


        //释放电机
        handBytesRelax = new byte[10];
        handBytesRelax[0] = 0XAD;
        handBytesRelax[1] = 0XAD;
        handBytesRelax[2] = 0X00;
        handBytesRelax[3] = 0X00;
        handBytesRelax[4] = 0X00;
        handBytesRelax[5] = 0X00;
        handBytesRelax[6] = 0X00;
        handBytesRelax[7] = 0X00;
        handBytesRelax[8] = 0XFF;
        handBytesRelax[9] = 0XFF;


        //正向锁定协议
        handBytesFore = new byte[15];
        handBytesFore[0] = 0XBD;
        handBytesFore[1] = 0XBD;
        handBytesFore[2] = 0X00;
        handBytesFore[3] = 0X00;
        handBytesFore[4] = 0X00;
        handBytesFore[5] = 0X00;
        handBytesFore[6] = 0X00;
        handBytesFore[7] = 0X00;
        handBytesFore[8] = 0X00;
        handBytesFore[9] = 0X00;
        handBytesFore[10] = 0X00;
        handBytesFore[11] = 0X00;
        handBytesFore[12] = 0X00;
        handBytesFore[13] = 0XFF;
        handBytesFore[14] = 0XFF;


        //初始化udp
        InitSocket();
    }


    void Start () {
      
    }
    
    
    void Update () {

    }

    /// <summary>
    /// 退出
    /// </summary>
    void OnApplicationQuit()
    {
        SocketQuit();
    }


    

    /// <summary>
    /// 发送触碰事件给手
    /// </summary>
    public void SocketInteraction() {
         handBytesColl[7] = ResultSumToByte(handBytesColl);

       // handBytes[9] = ResultSumToByte(handBytes);
        string str = ByteToHexStr(handBytesColl);
        //Debug.Log("发送的字节数组是:::"+str);

       
        SocketSendTo(handBytesColl);
    }

    /// <summary>
    /// 发送正向锁定协议给手柄
    /// </summary>
    public void SocketInteraction2() {
        SocketSendTo(handBytesColl);
    }




    ///获取校验位
    public byte ResultSumToByte(byte[] bytes)
    {
        int sum = 0;
        string str;
    
        for (int i = 2; i < bytes.Length - 3; i++) 
        {
          
            str = bytes[i].ToString("X");
            sum += Convert.ToInt32(str, 16);
        }
        Debug.Log("校验位数值:" + sum);
        byte b_t = Convert.ToByte(sum);
        return b_t;
    }




}

HandUdpClient

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System;


public class HandUdpClient : MonoBehaviour
{

    public string serverIP = "192.168.1.1";
    public int port = 8899;

    private Socket socket;
    private EndPoint serverEnd;
    private IPEndPoint ipEnd;
    private string recvStr;
    private string sendStr;
    private byte[] recvData = new byte[1024];
    private byte[] sendData = new byte[1024];
    private int recvLen;
    private Thread connectThread;
    private int tempI;

    //键指参数
    public float angleX_pinkie; //尾指
    public float angleY_pinkie;
    public float force_pinkie;


    public float angleX_ringfinger;//无名指
    public float angleY_ringfinger;
    public float force_ringfinger;

    public float angleX_midfinger;//中指
    public float angleY_midfinger;
    public float force_midfinger;

    public float angleX_indexfinger;//食指
    public float angleY_indexfinger;
    public float force_indexfinger;

    public float angleX_thumb;//大拇指
    public float angleY_thumb;
    public float force_thumb;

    //电量
    public float electricity;

    //对应手指的键 字节数  纵向 横向 力度
    public byte[] recvDataTemp= new byte[34];


    /// <summary>
    /// 初始化
    /// </summary>
    public void InitSocket()
    {

        ipEnd = new IPEndPoint(IPAddress.Parse(serverIP), port);
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
        serverEnd = (EndPoint)sender;
        print("Waiting for sending UDP dgram");
        SocketSend("Hello World");
        //SocketSendTo(new byte[15]); //0107

        connectThread = new Thread(new ThreadStart(SocketReceive));
        connectThread.IsBackground = true;
        connectThread.Start();
    }


    /// <summary>
    /// 发送数据给服务端
    /// </summary>
    public void SocketSend(string sendStr)
    {
        sendData = new byte[1024];
        sendData = Encoding.ASCII.GetBytes(sendStr);
        socket.SendTo(sendData, sendData.Length, SocketFlags.None, ipEnd);
    }


    /// <summary>
    /// 发送手指信息给服务端
    /// </summary>

    public void SocketSendTo(byte[] MsgData)
    {
      
        socket.SendTo(MsgData, MsgData.Length, SocketFlags.None, ipEnd);
    }




    /// <summary>
    /// 接收服务器数据
    /// </summary>
    public void SocketReceive()
    {

        while (true)
        {
            recvData = new byte[1024];
            recvLen = socket.ReceiveFrom(recvData, ref serverEnd);
            recvDataTemp = new byte[34]; //0104

            for (int i = 0; i < recvData.Length; i++)
            {
                if (recvData.Length >= 37 && recvData[i] == 0xBD && recvData[i + 1] == 0xBD && recvData[i + 35] == 0xFF && recvData[i + 36] == 0xFF)
                {
                    recvDataTemp = new byte[34];
                    tempI = i;
                   // print("接收服务端发来的数据:" + recvStr + "---------" + i);
                    for (int j = 0; j < recvDataTemp.Length; j++)
                    {
                        recvDataTemp[j] = recvData[i];
                        i++;
                    }
                    i = tempI;
                    recvStr = ByteToHexStr(recvDataTemp);
                   // print("接收服务端发来的数据:" + recvStr+"---------"+i);

                    angleX_pinkie = HexStrFloat(recvStr.Substring(4, 4)) ;
                    angleY_pinkie = HexStrFloat(recvStr.Substring(4 * 2, 4)) ;
                    force_pinkie = HexStrFloat(recvStr.Substring(4 * 3, 4)) ;


                    angleX_ringfinger = HexStrFloat(recvStr.Substring(4 * 4, 4));
                    angleY_ringfinger = HexStrFloat(recvStr.Substring(4 * 5, 4));
                    force_ringfinger = HexStrFloat(recvStr.Substring(4 * 6, 4));

                    angleX_midfinger = HexStrFloat(recvStr.Substring(4 * 7, 4));
                    angleY_midfinger = HexStrFloat(recvStr.Substring(4 * 8, 4));
                    force_midfinger = HexStrFloat(recvStr.Substring(4 * 9, 4));

                    angleX_indexfinger = HexStrFloat(recvStr.Substring(4 * 10, 4));
                    angleY_indexfinger = HexStrFloat(recvStr.Substring(4 * 11, 4));
                    force_indexfinger = HexStrFloat(recvStr.Substring(4 * 12, 4));

                    angleX_thumb = HexStrFloat(recvStr.Substring(4 * 13, 4));
                    angleY_thumb = HexStrFloat(recvStr.Substring(4 * 14, 4));
                    force_thumb = HexStrFloat(recvStr.Substring(4 * 15, 4));

                    electricity = HexStrFloat(recvStr.Substring(4 * 16, 4));
                }
            }

           // recvStr = null;
           // recvData = null;
           // Thread.Sleep(0); //重点0107
        }
    }


    /// <summary>
    /// 16进制字符串转字节数
    /// </summary>
    public  byte[] HexStrToByte(string hexString)
    {

        hexString = hexString.Replace(" ", "");
        if ((hexString.Length % 2) != 0)
            hexString += "";
        byte[] returnBytes = new byte[hexString.Length / 2];
        for (int i = 0; i < returnBytes.Length; i++)
        {
            returnBytes[i] = Convert.ToByte(Convert.ToInt32(hexString.Substring(i * 2, 2).Trim(), 16));
        }

        return returnBytes;
    }

    /// <summary>
    /// 字节数组转16进制字符串
    /// </summary>
    public  string ByteToHexStr(byte[] bytes)
    {
        string returnStr = "";
        if (bytes != null)
        {
            for (int i = 0; i < bytes.Length; i++)
            {
                returnStr += bytes[i].ToString("X2");
            }
        }

        return returnStr;
    }

    /// <summary>
    /// 16进制字符串转换成浮点型
    /// </summary>
    public  float HexStrFloat(string hexStr)
    {
        if (hexStr == null)
            return 0;

        UInt32 x = Convert.ToUInt32(hexStr, 16);
        float fy = x;
       // fy = fy * 90 / 900;
        return fy;
    }

    /// <summary>
    /// 将浮点型转换成两字节
    /// </summary>
    public byte[] GetHightByteLowByte(float num)
    {
        
        byte[] bs2 = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)num));// 注意
       // print("高字节:::" + (bs2[0].ToString("X2") + "    低字节::" + bs2[1].ToString("X2")));
        return bs2;
    }



    /// <summary>
    /// 获取16进制的byte[]数组的和与和的后两位校验值
    /// </summary>
    public byte GetCalibrationValue(byte[] dateByte)
    {
        byte byteTemp;
        int sum = 0;
        for (int j = 0; j < dateByte.Length - 1; j++)
        {
            string str1 = dateByte[j].ToString("x");
            sum += Convert.ToInt32(str1, 16);
        }

        sum = sum % 256; //12251622
        string sum16 = Convert.ToString(sum, 16);
        int sum16Length = sum16.Length;
        if (sum16Length >= 2)
        {
            byteTemp = (byte)Convert.ToInt32(sum16.Substring(sum16Length - 2), 16);
        }
        else
        {
            byteTemp = (byte)Convert.ToInt32((sum16), 16);
        }
        return byteTemp;
    }




   



    /// <summary>
    /// 连接关闭
    /// </summary>
    public void SocketQuit() {
        //关闭线程
        if (connectThread!=null)
        {
            connectThread.Interrupt();
            connectThread.Abort();
        }
        //最后关闭socket
        if (socket!=null)
        {
            socket.Close();
        }

    }

}

UIManager


using UnityEngine.UI;

public class UIManager : MonoBehaviour
{


    public Image fill; //背景图
    public Text mText; //显示文字
    public Slider slider;

    private float elec;


    void Update()
    {

        //显示数值
        elec = HandManager.instance.handSocket.electricity / 1000;
        // mText.text = (int)(elec/(slider.value)*100) + "%";
        mText.text = Mathf.Floor((elec / (slider.value) * 100)) + "%";
        // mText.text = elec + "";
        if (elec > 6)
        {
            fill.color = Color.green;
        }
        else if (elec > 3 && elec <= 6)
        {
            fill.color = Color.yellow;
        }
        else
        {
            fill.color = Color.red;
        }
    }
}

Items

Bottle



public class Bottle : MonoBehaviour
{

  

    public bool isThumbIn;
    public bool isIndexIn;


    /// <summary>
    /// 碰到
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerEnter(Collider collision)
    {
       //  Debug.Log("执行了碰撞事件"+ collision.transform.name);
        if (collision.transform.name == "R_thumb_end")
        {
            isThumbIn = true;
     
        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = true;
        }
        Grab();

    }

    //抓住
    void Grab()
    {
        if (isIndexIn&&isThumbIn)
        {
            gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
            gameObject.transform.localPosition = new Vector3(0.0105f, 0.097f, -0.0389f);
            gameObject.transform.localEulerAngles = new Vector3(-150, -100, 0);
            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
        }
    }





    /// <summary>
    /// 离开
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerExit(Collider collision)
    {

        if (collision.transform.name== "R_thumb_end") //
        {
            Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
            isThumbIn = false;

        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = false;
        }
        Relex();
    }


    //松开
    void Relex()
    {
        if (!isIndexIn&&!isThumbIn)
        {
            gameObject.transform.parent = null;
            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //解冻所有
        }
        
    }
}

Box



public class Box : MonoBehaviour
{

  

    public bool isThumbIn;
    public bool isIndexIn;

   // public  TrackObject trackObject;


    /// <summary>
    /// 碰到
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerEnter(Collider collision)
    {
       //  Debug.Log("执行了碰撞事件"+ collision.transform.name);
        if (collision.transform.name == "R_thumb_end")
        {
            isThumbIn = true;
     
        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = true;
        }
        Grab();

    }

    //抓住
    void Grab()
    {
        if (isIndexIn&&isThumbIn)
        {
            gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
           // gameObject.transform.localPosition = new Vector3(0.0575f, 0.0886f, -0.0455f);
           // gameObject.transform.localPosition = new Vector3(0.0515f, 0.106f, -0.0316f);
            //gameObject.transform.localEulerAngles = new Vector3(-45f, -60f, -112f);

            gameObject.transform.localPosition = new Vector3(0.0154f, 0.1241f, -0.0196f);
            gameObject.transform.localEulerAngles = new Vector3(51.688f, 122.983f, -245.338f);

            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
        }
    }

    /// <summary>
    /// 碰撞逗留   检测碰撞所有物体的名称
    /// </summary>
    void OnCollisionStay(Collision collision)
    {
        //Collider[] m_Collider = Physics.OverlapBox(transform.position, transform.GetComponent<BoxCollider>().size);
        //List<string> m_ColliderNames = new List<string>(); //定义一个集合存贮名称
        //for (int i = 0; i < m_Collider.Length; i++)
        //{
        //    Debug.Log("被碰撞物的名称::" + m_Collider[i].transform.name);
        //    m_ColliderNames.Add(m_Collider[i].transform.name);
        //}

        //if ((m_ColliderNames.Contains("R_index_end") || m_ColliderNames.Contains("R_middle_end") || m_ColliderNames.Contains("R_ring_end") || m_ColliderNames.Contains("R_pinky_end")) && m_ColliderNames.Contains("R_thumb_end"))
        //{

        //    gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
        //}
        //else
        //{
        //    gameObject.transform.parent = null;
        //    gameObject.transform.parent = null;
        //    gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //解冻所有
        //}
        //m_ColliderNames.Clear();

       // if (HandManager.instance.handSocket.force_thumb<20)
       // {
          //  isExit=true;
        //}


    }

    /// <summary>
    /// 离开
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerExit(Collider collision)
    {

        if (collision.transform.name== "R_thumb_end") //
        {
            Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
            isThumbIn = false;

        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = false;
        }
        Relex();
    }


    //松开
    void Relex()
    {
        if (!isIndexIn&&!isThumbIn)
        {
            gameObject.transform.parent = null;
            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //解冻所有

            // gameObject.transform.GetComponent<Rigidbody>().AddForce(-trackObject.hand.velocity,ForceMode.Impulse);
            // gameObject.transform.GetComponent<Rigidbody>().AddForceAtPosition(Vector3.right- trackObject.hand.velocity, gameObject.transform.position,ForceMode.VelocityChange);

          //  Debug.Log("tracker速度::"+ trackObject.hand.velocity);
           // gameObject.transform.GetComponent<Rigidbody>().velocity = trackObject.hand.velocity; //刚体速度=tracker速度


        }
        

    }
}

Container



public class Container : MonoBehaviour
{

  

    public bool isThumbIn;
    public bool isIndexIn;

    /// <summary>
    /// 碰到
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerEnter(Collider collision)
    {
       //  Debug.Log("执行了碰撞事件"+ collision.transform.name);
        if (collision.transform.name == "R_thumb_end")
        {
            isThumbIn = true;
     
        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = true;
        }
        Grab();

    }

    //抓住
    void Grab()
    {
        if (isIndexIn&&isThumbIn)
        {
            gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
           // gameObject.transform.localPosition = new Vector3(0.0575f, 0.0886f, -0.0455f);
           // gameObject.transform.localPosition = new Vector3(0.0515f, 0.106f, -0.0316f);
            //gameObject.transform.localEulerAngles = new Vector3(-45f, -60f, -112f);

            gameObject.transform.localPosition = new Vector3(0.0154f, 0.1241f, -0.0196f);
            gameObject.transform.localEulerAngles = new Vector3(51.688f, 122.983f, -245.338f);

            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
        }
    }

    /// <summary>
    /// 碰撞逗留   检测碰撞所有物体的名称
    /// </summary>
    void OnCollisionStay(Collision collision)
    {
        //Collider[] m_Collider = Physics.OverlapBox(transform.position, transform.GetComponent<BoxCollider>().size);
        //List<string> m_ColliderNames = new List<string>(); //定义一个集合存贮名称
        //for (int i = 0; i < m_Collider.Length; i++)
        //{
        //    Debug.Log("被碰撞物的名称::" + m_Collider[i].transform.name);
        //    m_ColliderNames.Add(m_Collider[i].transform.name);
        //}

        //if ((m_ColliderNames.Contains("R_index_end") || m_ColliderNames.Contains("R_middle_end") || m_ColliderNames.Contains("R_ring_end") || m_ColliderNames.Contains("R_pinky_end")) && m_ColliderNames.Contains("R_thumb_end"))
        //{

        //    gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
        //}
        //else
        //{
        //    gameObject.transform.parent = null;
        //    gameObject.transform.parent = null;
        //    gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //解冻所有
        //}
        //m_ColliderNames.Clear();

       // if (HandManager.instance.handSocket.force_thumb<20)
       // {
          //  isExit=true;
        //}


    }

    




    /// <summary>
    /// 离开
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerExit(Collider collision)
    {

        if (collision.transform.name== "R_thumb_end") //
        {
            Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
            isThumbIn = false;

        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = false;
        }
        Relex();
    }


    //松开
    void Relex()
    {
        if (!isIndexIn&&!isThumbIn)
        {
            gameObject.transform.parent = null;
            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //解冻所有
        }
        
    }
}


Heart



public class Heart : MonoBehaviour {

   
    public bool isThumbIn; //拇指
    public bool isIndexIn; //食指


    /// <summary>
    /// 碰到
    /// </summary>
    /// <param name="collision"></param>
    void OnTriggerEnter(Collider collision)
    {
        //  Debug.Log("执行了碰撞事件"+ collision.transform.name);
        if (collision.transform.name == "R_thumb_end")
        {
            isThumbIn = true;
        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = true;
        }
        Grab();

    }

    void Grab()
    {
        if (isIndexIn && isThumbIn)
        {
            HandManager.instance.isTouch = true;
            gameObject.transform.parent = GameObject.Find("Hand_Right").transform;
            //gameObject.transform.localPosition = new Vector3(0.0019f, 0.1f, -0.0277f);//位置锁定
            //gameObject.transform.localEulerAngles = new Vector3(0, 0, -0);
            gameObject.transform.localPosition = new Vector3(0.0019f, 0.1f, -0.0277f);//位置锁定
            gameObject.transform.localEulerAngles = new Vector3(-2.541f, 56.72f, 27.083f);
            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
        }
    }

    ///// <summary>
    ///// 碰到
    ///// </summary>
    ///// <param name="collision"></param>
    //void OnCollisionEnter(Collision collision)
    //{
    //    // Debug.Log("执行了碰撞事件");
    //    if (collision.transform.tag == "Hand")
    //    {
    //       // collision.transform.GetComponent<Collider>().enabled = false;
    //        gameObject.transform.parent = collision.transform;
    //        gameObject.transform.localPosition = new Vector3(0.0105f, 0.097f, -0.0389f);
    //       // gameObject.transform.localEulerAngles = new Vector3(-150, 100, 0);
    //       // gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; //冻结所有
    //    }

    //}


    void OnTriggerExit(Collider collision)
    {

        if (collision.transform.name == "R_thumb_end") //
        {
            Debug.Log("瓶子碰撞的离开的名字:::" + collision.transform.name);
            //collision.transform.GetComponent<Collider>().enabled = false;
            isThumbIn = false;

        }
        if (collision.transform.name == "R_index_end")
        {
            isIndexIn = false;
        }
        Relex();
    }

    void Relex()
    {
        if (!isIndexIn && !isThumbIn)
        {
            HandManager.instance.isTouch = false;
            gameObject.transform.parent = null;
            //gameObject.AddComponent<Rigidbody>();
            gameObject.transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //解冻所有
        }

    }



}

HeartJitter


using DG.Tweening;

public class HeartJitter : MonoBehaviour {

    private float timer;

    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {

        timer += Time.deltaTime;
        if (timer >= 0.5f)
        {
            transform.DOScale(new Vector3(0.1f, 0.2f, 0.1f), 1f);
            
        }
        if (timer >= 1f)
        {
            transform.transform.DOScale(new Vector3(0.2f, 0.5f, 0.2f), 1f);
            timer = 0;
        }
        


        // StartCoroutine(DelayShow());

    }

    IEnumerator DelayShow() {
        transform.DOScale(new Vector3(0.005f, 0.005f, 0.005f), 0.5f);
        yield return new WaitForSeconds(1f);
        transform.transform.DOScale(new Vector3(1f, 1f, 1f), 1f);
    }

}

Finger


public enum FingerIndex
{
    Thumb,
    Index,
    Middle,
    Ring,
    Pinky,
}
public class Finger : MonoBehaviour
{
    public FingerIndex index;
    public GameObject fingerRoot;  //持有拇指的引用

    //记录旋转角度
    private Quaternion fingerRotate;

    private float fingerRotz; //拇指
    private float fingerZchild;  //拇指的子物体
    private float tempFingerZ; //存放值

    //拇指锁定协议
    public byte byteFinger_H;
    public byte byteFinger_L;

    public bool isTouch_finger; //是否触碰到拇指
    public float elasticFinger;//拇指的弹性变化度
    public float elasticFingerTemp;//拇指的弹性变化度
    public byte[] byteFinger; //存贮拇指的高字节和低字节

    //public float timer=0; //计时器



    public bool isExit;

    void Awake()
    {

        fingerRotate = fingerRoot.transform.rotation;

        fingerRotz = fingerRoot.transform.localEulerAngles.z;
        fingerZchild = fingerRoot.transform.GetChild(0).GetChild(0).localEulerAngles.z;
        tempFingerZ = fingerZchild;

    }

    void Start()
    {

        //实时获取数据同步
    }


    void Update()
    {

    }

    /// <summary>
    /// 物理检测手指
    /// </summary>
    void FixedUpdate()
    {

        Ctrl_thumb();

        if (isTouch_finger)
        {
            // HandManager.instance.timer += Time.deltaTime;
            if (HandManager.instance.timer >= 0f && HandManager.instance.timer < 1f)
            {
                //elasticThumb = elasticThumbTemp + 50;
                elasticFinger += 5;
                Debug.Log("0.5f拇指" + elasticFinger);
            }

            if (HandManager.instance.timer >= 1f && HandManager.instance.timer < 2f)
            {
                //elasticThumb = elasticThumbTemp - 50;
                elasticFinger -= 5;
                Debug.Log("1f拇指:" + elasticFinger);
            }

            //if (HandManager.instance.timer >=1f)
            //{
            //   HandManager.instance.timer = 0;
            //    Debug.Log("拇指时间归零");
            //}


            byteFinger = HandManager.instance.handSocket.GetHightByteLowByte(elasticFinger);
            byteFinger_H = byteFinger[0];
            byteFinger_L = byteFinger[1];

        }
        else
        {
            // byteThumb_H = 0X00;
            // byteThumb_L = 0X00;

        }


    }




    /// <summary>
    /// 进入触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerEnter(Collider coll)
    {
        //if (coll.gameObject.name == Tags.Sphere)
        //if (coll.gameObject.name == Tags.Cylinder)
        if (coll.gameObject.name != null)
        {

            if (coll.gameObject.tag == "Heart")
            {
                isTouch_finger = true;

                elasticFinger = HandManager.instance.handSocket.angleX_thumb;
                elasticFingerTemp = elasticFinger;
            }
            else
            {
                byteFinger_H = HandManager.instance.handSocket.recvDataTemp[26];
                byteFinger_L = HandManager.instance.handSocket.recvDataTemp[27];
            }


            if (coll.gameObject.name == "R_thumb_end")
            {
                isExit = false;
            }

        }
    }

    /// <summary>
    /// 离开触发器
    /// </summary>
    /// <param name="coll"></param>
    void OnTriggerExit(Collider coll)
    {

        // if (coll.gameObject.name == Tags.Sphere)
        // if (coll.gameObject.name == Tags.Cylinder)


        if (coll.gameObject.name != null)
        {
            byteFinger_H = 0X00;
            byteFinger_L = 0X00;
            isTouch_finger = false;
        }

        if (coll.gameObject.name == "R_thumb_end")
        {
            isExit = true;
        }

    }


    void OnApplicationQuit()
    {

        fingerRoot.transform.rotation = fingerRotate;

    }
    /// <summary>
    /// 拇指
    /// </summary>
    protected virtual void Ctrl_thumb()
    {

        //thumb.transform.localEulerAngles = new Vector3(thumb.transform.localEulerAngles.x, Map(angleY_thumb, 693, 769, -35, 40), MapData(angleX_thumb, 400, 730, -26.361f, -115.18f));
        //fingerRoot.transform.localEulerAngles = new Vector3(fingerRoot.transform.localEulerAngles.x, fingerRoot.transform.localEulerAngles.y, MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f) + 25);
        //if (fingerRoot.transform.localEulerAngles.z - 800 < fingerRotz)
        //{

        //    tempFingerZ = MapData(HandManager.instance.handSocket.angleX_thumb, 400, 730, -26.361f, -115.18f);
        //}
        //else
        //{
        //    tempFingerZ = fingerZchild;
        //}
        //fingerRoot.transform.GetChild(0).localEulerAngles = new Vector3(fingerRoot.transform.GetChild(0).localEulerAngles.x, fingerRoot.transform.GetChild(0).localEulerAngles.y, tempFingerZ + 45);

        // str = "Y轴:   " + MapData(angleY_thumb, 693, 769, -35, 40) + "       Z轴::" + MapData(angleX_thumb, 400, 730, -26.361f, -115.18f);



    }



    public float MapData(float value, float min_in, float max_in, float min_out, float max_out)
    {

        if (min_in > max_in)
        {
            min_in = min_in + max_in;
            max_in = min_in - max_in;
            min_in = min_in - max_in;

            min_out = min_out + max_out;
            max_out = min_out - max_out;
            min_out = min_out - max_out;

        }

        if (value < min_in)
            value = min_in;
        if (value > max_in)
            value = max_in;

        return (value - min_in) / (max_in - min_in) * (max_out - min_out) + min_out;
    }
}

GrabableObj



public class GrabableObj : MonoBehaviour {

    public bool isLockPos;
    public Vector3 lockPos;

    public bool isLockRot;
    public Vector3 lockRot;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    public void Grab()
    {
        if (isLockPos)
        {
            transform.localPosition = lockPos;
        }
        if (isLockRot)
        {
            transform.localEulerAngles = lockRot;
        }
    }
}

heartAni

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class heartAni : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }
    
    public void Bigger()
    {
        HandManager.instance.timer = 0;
    }
    public void Smaller()
    {
        HandManager.instance.timer = 1;

    }
}

TrackObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrackObject : MonoBehaviour {

    public  SteamVR_Controller.Device hand;

    public float speed = 2;   //速度值随意设

   // SteamVR_TrackedObject trackObject;

    void Start () {
        //  trackObject = this.GetComponent<SteamVR_TrackedObject>();



        var rightHands = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);  //获得右手柄         
        hand =SteamVR_Controller.Input(rightHands);

    }
    
    
    void Update () {
        //SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackObject.index);

        //if (device.GetHairTrigger())
        //{
        //    Debug.Log("GetHairTrigger");
        //}
        //if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
        //{  //.Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger

        //    Debug.Log("ExportSteamVR_Trigger");
        //}

       

    }


    void FixedUpdate()
    {//方法一:         
        if (hand.velocity.magnitude > speed)
        {
           // Debug.Log("手柄速度:::::"+ hand.velocity.magnitude);
        }
        //方法二          
        //if (Vector3.Distance(hand.velocity, Vector3.zero) > speed)
        //{
        //    Debug.Log("手柄速度");
        //}

    }
}

三、效果图


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容