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

            part.rigidbody = go.GetComponent <Rigidbody>();
            if (part.rigidbody == null)
            {
                part.rigidbody = go.AddComponent <Rigidbody>();
            }
            part.rigidbody.mass                   = mass;
            part.rigidbody.drag                   = ragdollProperties.rigidDrag;
            part.rigidbody.angularDrag            = ragdollProperties.rigidAngularDrag;
            part.rigidbody.collisionDetectionMode = ragdollProperties.cdMode;
            part.rigidbody.isKinematic            = ragdollProperties.isKinematic;
            part.rigidbody.useGravity             = ragdollProperties.useGravity;

            if (addJoint)
            {
                part.joint = go.GetComponent <CharacterJoint>();
                if (part.joint == null)
                {
                    part.joint = go.AddComponent <CharacterJoint>();
                }

                part.joint.enablePreprocessing = false;
                part.joint.enableProjection    = true;
            }
        }
Esempio n. 2
0
        static void AddComponentesTo(RagdollPartSphere part, RagdollProperties ragdollProperties, float mass, bool addJoint)
        {
            AddComponentesToBase(part, ragdollProperties, mass, addJoint);
            GameObject go = part.transform.gameObject;

            part.collider = GetCollider <SphereCollider>(go.transform);
            if (part.collider == null)
            {
                part.collider = go.AddComponent <SphereCollider>();
            }
            part.collider.isTrigger = ragdollProperties.asTrigger;
        }
Esempio n. 3
0
        /// <summary>
        /// Create all ragdoll's components and set their proterties
        /// </summary>
        public void ApplyRagdoll(float totalMass, RagdollProperties ragdollProperties)
        {
            if (!_readyToGenerate)
            {
                Debug.LogError("Initialization failed. Reinstance object!");
                return;
            }

            var weight = new WeightCalculator(totalMass, ragdollProperties.createTips);

            bool alreadyRagdolled = _pelvis.transform.gameObject.GetComponent <Rigidbody>() != null;

            AddComponentesTo(_pelvis, ragdollProperties, weight.Pelvis, false);
            AddComponentesTo(_leftHip, ragdollProperties, weight.Hip, true);
            AddComponentesTo(_leftKnee, ragdollProperties, weight.Knee, true);
            AddComponentesTo(_rightHip, ragdollProperties, weight.Hip, true);
            AddComponentesTo(_rightKnee, ragdollProperties, weight.Knee, true);
            AddComponentesTo(_leftArm, ragdollProperties, weight.Arm, true);
            AddComponentesTo(_leftElbow, ragdollProperties, weight.Elbow, true);
            AddComponentesTo(_rightArm, ragdollProperties, weight.Arm, true);
            AddComponentesTo(_rightElbow, ragdollProperties, weight.Elbow, true);
            AddComponentesTo(_chest, ragdollProperties, weight.Chest, true);
            AddComponentesTo(_head, ragdollProperties, weight.Head, true);

            if (ragdollProperties.createTips)
            {
                AddComponentesTo(_leftFoot, ragdollProperties, weight.Foot, true);
                AddComponentesTo(_rightFoot, ragdollProperties, weight.Foot, true);
                AddComponentesTo(_leftHand, ragdollProperties, weight.Hand, true);
                AddComponentesTo(_rightHand, ragdollProperties, weight.Hand, true);
            }

            if (alreadyRagdolled)
            {
                return;
            }

            // Pelvis
            Vector3 pelvisSize   = new Vector3(0.32f, 0.31f, 0.3f);
            Vector3 pelvisCenter = new Vector3(00f, 0.06f, -0.01f);

            _pelvis.collider.size   = Abs(_pelvis.transform.InverseTransformVector(pelvisSize));
            _pelvis.collider.center = _pelvis.transform.InverseTransformVector(pelvisCenter);

            ApplySide(true, ragdollProperties.createTips);
            ApplySide(false, ragdollProperties.createTips);

            // Chest collider
            Vector3 chestSize = new Vector3(0.34f, 0.34f, 0.28f);

            float y = (pelvisSize.y + chestSize.y) / 2f + pelvisCenter.y;

            y -= _chest.transform.position.y - _pelvis.transform.position.y;
            _chest.collider.size   = Abs(_chest.transform.InverseTransformVector(chestSize));
            _chest.collider.center = _chest.transform.InverseTransformVector(new Vector3(0f, y, -0.03f));

            // Chest joint
            var chestJoint = _chest.joint;

            ConfigureJointParams(_chest, _pelvis.rigidbody, _rootNode.right, _rootNode.forward);
            ConfigureJointLimits(chestJoint, -45f, 20f, 20f, 20f);

            // head
            _head.collider.radius = 0.1f;
            _head.collider.center = _head.transform.InverseTransformVector(new Vector3(0f, 0.09f, 0.03f));
            var headJoint = _head.joint;

            ConfigureJointParams(_head, _chest.rigidbody, _rootNode.right, _rootNode.forward);
            ConfigureJointLimits(headJoint, -45f, 20f, 20f, 20f);
        }