Esempio n. 1
0
        static void AddComponentesTo(RagdollPartBox part, RagdollProperties ragdollProperties, float mass, bool addJoint)
        {
            AddComponentesToBase(part, ragdollProperties, mass, addJoint);
            GameObject go = part.transform.gameObject;

            part.collider = GetCollider <BoxCollider>(go.transform);
            if (part.collider == null)
            {
                part.collider = go.AddComponent <BoxCollider>();
            }
            part.collider.isTrigger = ragdollProperties.asTrigger;
        }
Esempio n. 2
0
        /// <summary>
        /// Configure one hand and one leg
        /// </summary>
        /// <param name="leftSide">If true, configuration apply to left hand and left leg, otherwise right hand and right leg</param>
        void ApplySide(bool leftSide, bool createTips)
        {
            RagdollPartCapsule hip  = (leftSide ? _leftHip : _rightHip);
            RagdollPartCapsule knee = (leftSide ? _leftKnee : _rightKnee);
            RagdollPartBox     foot = (leftSide ? _leftFoot : _rightFoot);

            RagdollPartCapsule arm   = (leftSide ? _leftArm : _rightArm);
            RagdollPartCapsule elbow = (leftSide ? _leftElbow : _rightElbow);
            RagdollPartBox     hand  = (leftSide ? _leftHand : _rightHand);

            ConfigureRagdollForLimb(hip, knee, foot, createTips);
            ConfigureLegsJoints(hip, knee, foot, createTips);

            ConfigureRagdollForLimb(arm, elbow, hand, createTips);
            ConfigureHandJoints(arm, elbow, hand, leftSide, createTips);
        }
Esempio n. 3
0
        public Ragdoller(Transform player, Vector3 playerDirection)
        {
            _playerDirection = playerDirection;
            _readyToGenerate = false;

            // find Animator
            Animator animator = FindAnimator(player);

            if (animator == null)
            {
                return;
            }
            _rootNode = animator.transform;

            // specify all parts of ragdoll
            _pelvis     = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.Hips));
            _leftHip    = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg));
            _leftKnee   = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg));
            _rightHip   = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.RightUpperLeg));
            _rightKnee  = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.RightLowerLeg));
            _leftArm    = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.LeftUpperArm));
            _leftElbow  = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.LeftLowerArm));
            _rightArm   = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.RightUpperArm));
            _rightElbow = new RagdollPartCapsule(animator.GetBoneTransform(HumanBodyBones.RightLowerArm));
            _chest      = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.Chest));
            _head       = new RagdollPartSphere(animator.GetBoneTransform(HumanBodyBones.Head));

            _leftFoot  = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.LeftFoot));
            _rightFoot = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.RightFoot));
            _leftHand  = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.LeftHand));
            _rightHand = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.RightHand));

            if (_chest.transform == null)
            {
                _chest = new RagdollPartBox(animator.GetBoneTransform(HumanBodyBones.Spine));
            }

            if (!CheckFields())
            {
                Debug.LogError("Not all nodes was found!");
                return;
            }

            _readyToGenerate = true;
        }
Esempio n. 4
0
        void ConfigureLegsJoints(RagdollPartCapsule hip, RagdollPartCapsule knee, RagdollPartBox foot, bool createTips)
        {
            var hipJoint  = hip.joint;
            var kneeJoint = knee.joint;
            var footJoint = foot.joint;

            ConfigureJointParams(hip, _pelvis.rigidbody, _rootNode.right, _rootNode.forward);
            ConfigureJointParams(knee, hip.rigidbody, _rootNode.right, _rootNode.forward);

            ConfigureJointLimits(hipJoint, -10f, 120f, 90f, 20f);
            ConfigureJointLimits(kneeJoint, -120f, 0f, 10f, 20f);

            if (createTips)
            {
                ConfigureJointParams(foot, knee.rigidbody, _rootNode.right, _rootNode.forward);
                ConfigureJointLimits(footJoint, -70f, 70f, 45f, 20f);
            }
        }
Esempio n. 5
0
        void ConfigureHandJoints(RagdollPartCapsule arm, RagdollPartCapsule elbow, RagdollPartBox hand, bool leftHand, bool createTips)
        {
            var dirUpper = elbow.transform.position - arm.transform.position;
            var dirLower = hand.transform.position - elbow.transform.position;
            var dirHand  = GetLongestTransform(hand.transform).position - hand.transform.position;            // TODO: need to find the most longest bone

            if (leftHand)
            {
                ConfigureJointLimits(arm.joint, -100f, 30f, 100f, 45f);
                ConfigureJointLimits(elbow.joint, -120f, 0f, 10f, 90f);
                if (createTips)
                {
                    ConfigureJointLimits(hand.joint, -90f, 90f, 90f, 45f);
                }
                dirUpper = -dirUpper;
                dirLower = -dirLower;
                dirHand  = -dirHand;
            }
            else
            {
                ConfigureJointLimits(arm.joint, -30f, 100f, 100f, 45f);
                ConfigureJointLimits(elbow.joint, 0f, 120f, 10f, 90f);
                if (createTips)
                {
                    ConfigureJointLimits(hand.joint, -90f, 90f, 90f, 45f);
                }
            }

            var upU = Vector3.Cross(_playerDirection, dirUpper);
            var upL = Vector3.Cross(_playerDirection, dirLower);
            var upH = Vector3.Cross(_playerDirection, dirHand);

            ConfigureJointParams(arm, _chest.rigidbody, upU, _playerDirection);
            ConfigureJointParams(elbow, arm.rigidbody, upL, _playerDirection);
            if (createTips)
            {
                ConfigureJointParams(hand, elbow.rigidbody, upH, _playerDirection);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Configer one of 4 body parts: right leg, left leg, right hand or left hand
        /// </summary>
        static void ConfigureRagdollForLimb(RagdollPartCapsule limbUpper, RagdollPartCapsule limbLower, RagdollPartBox tip, bool createTips)
        {
            float totalLength = limbUpper.transform.InverseTransformPoint(tip.transform.position).magnitude;

            // limbUpper
            CapsuleCollider upperCapsule = limbUpper.collider;
            var             boneEndPos   = limbUpper.transform.InverseTransformPoint(limbLower.transform.position);

            upperCapsule.direction = GetXyzDirection(limbLower.transform.localPosition);
            upperCapsule.radius    = totalLength * 0.12f;
            upperCapsule.height    = boneEndPos.magnitude;
            upperCapsule.center    = Vector3.Scale(boneEndPos, Vector3.one * 0.5f);

            // limbLower
            CapsuleCollider endCapsule = limbLower.collider;

            boneEndPos           = limbLower.transform.InverseTransformPoint(tip.transform.position);
            endCapsule.direction = GetXyzDirection(boneEndPos);
            endCapsule.radius    = totalLength * 0.12f;
            endCapsule.height    = boneEndPos.magnitude;
            endCapsule.center    = Vector3.Scale(boneEndPos, Vector3.one * 0.5f);

            // tip
            if (createTips)
            {
                boneEndPos = GetLongestTransform(tip.transform).position;
                boneEndPos = tip.transform.InverseTransformPoint(boneEndPos);

                Vector3 tipDir   = GetXyzDirectionV(boneEndPos);
                Vector3 tipSides = (tipDir - Vector3.one) * -1;
                Vector3 boxSize  = tipDir * boneEndPos.magnitude * 1.3f + tipSides * totalLength * 0.2f;

                BoxCollider tipBox = tip.collider;
                tipBox.size = boxSize;

                float halfTipLength = boneEndPos.magnitude / 2f;
                tipBox.center = Vector3.Scale(boneEndPos.normalized, Vector3.one * halfTipLength);
            }
        }