public void UnbendFinger(FingersType fingerType)
    {
        if (!EnableControl)
        {
            return;
        }

        byte fingerParentId = (byte)fingerType;

        for (int i = 0; i < HandParts[fingerParentId].Length; i++)
        {
            HandParts[fingerParentId][i].StopBending();
        }
    }
Esempio n. 2
0
    void InitHand() //Initialize ang configure bones of this hand
    {
        //Initalize HandPart array
        #region InitArray

        HandParts = new HandPart[6][];

        HandParts[0] = new HandPart[1];
        for (int i = 1; i < HandParts.Length; i++)
        {
            HandParts[i] = new HandPart[3];
        }
        #endregion

        //Find all hand parts by name of this transform
        #region HandPartsFinding

        HandParts[0][0] = transform.Find("Wrist").gameObject.AddComponent <HandPart>();
        for (int i = 1; i < HandParts.Length; i++)
        {
            FingersType fingerType = (FingersType)i;
            HandParts[i][0]        = HandParts[0][0].transform.Find(fingerType.ToString() + "0").gameObject.AddComponent <HandPart>();
            HandParts[i][0].IsRoot = true;

            for (int j = 1; j < HandParts[i].Length; j++)
            {
                HandParts[i][j] = HandParts[i][j - 1].transform.Find(fingerType.ToString() + j).gameObject.AddComponent <HandPart>();

                HandParts[i][j].PrevFingerBone     = HandParts[i][j - 1];
                HandParts[i][j - 1].NextFingerBone = HandParts[i][j];
            }
        }
        #endregion

        //Create collision detectors for all fingers
        #region CollisionDetectors                                                   //  ??????????????????????????????????????????????????

        for (int i = 1; i < HandParts.Length; i++)
        {
            FingersType fingerType = (FingersType)i;

            for (int j = 0; j < HandParts[i].Length; j++)
            {
                if (j == 0)
                {
                    continue;
                }
                GameObject collisionDetector = (GameObject)Instantiate(HandParts[i][j].gameObject);
                Destroy(collisionDetector.GetComponent <HandPart>());

                var children = new List <GameObject>();
                foreach (Transform child in collisionDetector.transform)
                {
                    children.Add(child.gameObject);
                }
                children.ForEach(child => Destroy(child));

                collisionDetector.name                    = fingerType.ToString() + j + "_colDetector";
                collisionDetector.transform.parent        = HandParts[i][j].transform;
                collisionDetector.transform.localPosition = Vector3.zero;
                collisionDetector.transform.localRotation = Quaternion.identity;
                collisionDetector.transform.localScale    = HandParts[i][j].transform.localScale;

                CapsuleCollider collider = collisionDetector.GetComponent <CapsuleCollider>();
                collider.isTrigger = true;

                collisionDetector.AddComponent <FingerCollisionDetector>();
                collisionDetector.GetComponent <FingerCollisionDetector>().ThisHandPart = HandParts[i][j];
                //HandParts[i][j].gameObject.GetComponent<FingerCollisionDetector>().ThisHandPart = HandParts[i][j];
            }
        }

        #endregion
        //Ignore collisions between all hand parts
        #region IgnoreCollisions

        foreach (var handPartParent in HandParts)
        {
            foreach (var handPart in handPartParent)
            {
                for (int i = 0; i < HandParts.Length; i++)
                {
                    for (int j = 0; j < HandParts[i].Length; j++)
                    {
                        if (HandParts[i][j] != handPart)
                        {
                            //Physics.IgnoreCollision(HandParts[i][j].collider, handPart.collider);
                            Physics.IgnoreCollision(HandParts[i][j].GetComponent <Collider>(), handPart.GetComponent <Collider>());
                        }
                    }
                }
            }
        }
        #endregion
    }
    void InitHand() //Initialize ang configure bones of this hand
    {
        //Initalize HandPart array
        #region InitArray

        HandParts = new HandPart[6][];

        HandParts[0] = new HandPart[2];
        for (int i = 1; i < HandParts.Length; i++)
        {
            HandParts[i] = new HandPart[3];
        }
        #endregion

        //Find all hand parts by name of this transform
        #region HandPartsFinding

        HandParts[0][0] = transform.Find("Forearm").gameObject.AddComponent <HandPart>();
        HandParts[0][1] = HandParts[0][0].transform.Find("Wrist").gameObject.AddComponent <HandPart>();
        _forearmJoint   = HandParts[0][0].GetComponent <ConfigurableJoint>();
        _wristJoint     = HandParts[0][1].GetComponent <ConfigurableJoint>();

        for (int i = 1; i < HandParts.Length; i++)
        {
            FingersType fingerType = (FingersType)i;

            HandParts[i][0]        = HandParts[0][1].transform.Find(fingerType.ToString() + "0").gameObject.AddComponent <HandPart>();
            HandParts[i][0].IsRoot = true;

            for (int j = 1; j < HandParts[i].Length; j++)
            {
                HandParts[i][j] = HandParts[i][j - 1].transform.Find(fingerType.ToString() + j).gameObject.AddComponent <HandPart>();

                HandParts[i][j].PrevFingerBone     = HandParts[i][j - 1];
                HandParts[i][j - 1].NextFingerBone = HandParts[i][j];
            }
        }
        #endregion

        //Create collision detectors for all fingers
        #region CollisionDetectors

        for (int i = 1; i < HandParts.Length; i++)
        {
            FingersType fingerType = (FingersType)i;

            for (int j = 0; j < HandParts[i].Length; j++)
            {
                if (j == 0)
                {
                    continue;
                }
                GameObject collisionDetector = (GameObject)Instantiate(HandParts[i][j].gameObject);
                Destroy(collisionDetector.GetComponent <HandPart>());

                var children = new List <GameObject>();
                foreach (Transform child in collisionDetector.transform)
                {
                    children.Add(child.gameObject);
                }
                children.ForEach(child => Destroy(child));

                collisionDetector.name                    = fingerType.ToString() + j + "_colDetector";
                collisionDetector.transform.parent        = HandParts[i][j].transform;
                collisionDetector.transform.localPosition = Vector3.zero;
                collisionDetector.transform.localRotation = Quaternion.identity;

                CapsuleCollider collider = collisionDetector.GetComponent <CapsuleCollider>();
                collider.isTrigger = true;
                collider.radius   *= 0.8f;
                collider.center    = new Vector3(-collider.center.x, 0, collider.center.z + 0.05f);
                collider.height   *= 0.8f;

                collisionDetector.AddComponent <FingerCollisionDetector>();
                collisionDetector.GetComponent <FingerCollisionDetector>().ThisHandPart = HandParts[i][j];
            }
        }
        #endregion

        //Set target local rotation for all fingers
        #region SetTargetRotation

        HandParts[1][0].TargetRotation = Quaternion.Euler(300, -15, 315);
        HandParts[1][1].TargetRotation = Quaternion.Euler(-10, 315, 12.5f);
        HandParts[1][2].TargetRotation = Quaternion.Euler(5, 320, 10);

        float indexLocalRotationX = -5;
        float indexLocalRotationZ = 8;

        for (int i = 2; i < HandParts.Length; i++)
        {
            for (int j = 0; j < HandParts[i].Length; j++)
            {
                if (j == 0)
                {
                    HandParts[i][j].TargetRotation = Quaternion.Euler(indexLocalRotationX, -60, indexLocalRotationZ);
                }
                else
                {
                    HandParts[i][j].TargetRotation = Quaternion.Euler(0, -60, 0);
                }
            }
            indexLocalRotationX += 2.5f;
            indexLocalRotationZ -= 3f;
        }
        #endregion

        //Ignore collisions between all hand parts
        #region IgnoreCollisions

        foreach (var handPartParent in HandParts)
        {
            foreach (var handPart in handPartParent)
            {
                for (int i = 0; i < HandParts.Length; i++)
                {
                    for (int j = 0; j < HandParts[i].Length; j++)
                    {
                        if (HandParts[i][j] != handPart)
                        {
                            Physics.IgnoreCollision(HandParts[i][j].collider, handPart.collider);
                        }
                    }
                }
            }
        }
        #endregion
    }