Example #1
0
        private void BuildColliderBox(BoneData bone, bool below)
        {
            Transform spine  = this.bones[(int)Bone.Spine].anchor;
            Vector3   axisUp = bone.anchor.TransformDirection(Vector3.up);

            Transform[] limbs = new Transform[]
            {
                this.bones[(int)Bone.L_Hip].anchor,
                this.bones[(int)Bone.R_Hip].anchor,
                this.bones[(int)Bone.L_Arm].anchor,
                this.bones[(int)Bone.R_Arm].anchor,
            };

            Bounds bounds = RagdollUtilities.GetBounds(bone.anchor, limbs);

            bounds = RagdollUtilities.ProportionalBounds(bounds);
            bounds = RagdollUtilities.Clip(bounds, bone.anchor, spine, axisUp, below);

            BoxCollider boxCollider = bone.anchor.gameObject.AddComponent <BoxCollider>();

            boxCollider.enabled = false;
            boxCollider.center  = bounds.center;
            boxCollider.size    = bounds.size;
            bone.collider       = boxCollider;
        }
Example #2
0
        private void BuildColliderCapsule(BoneData bone)
        {
            int   direction = 0;
            float distance  = 0.0f;

            if (bone.children.Count == 1)
            {
                Vector3 endPoint = this.bones[(int)bone.children[0]].anchor.position;
                RagdollUtilities.GetDirection(
                    bone.anchor.InverseTransformPoint(endPoint),
                    out direction, out distance
                    );
            }
            else
            {
                Vector3 length   = bone.anchor.position - this.bones[(int)bone.parent].anchor.position;
                Vector3 endPoint = bone.anchor.position + length;

                RagdollUtilities.GetDirection(
                    bone.anchor.InverseTransformPoint(endPoint),
                    out direction, out distance
                    );

                if (bone.anchor.GetComponentsInChildren(typeof(Transform)).Length > 1)
                {
                    Bounds bounds = RagdollUtilities.GetBounds(
                        bone.anchor,
                        bone.anchor.GetComponentsInChildren <Transform>()
                        );

                    if (distance > 0f)
                    {
                        distance = bounds.max[direction];
                    }
                    if (distance < 0f)
                    {
                        distance = bounds.min[direction];
                    }
                }
            }

            CapsuleCollider capsuleCollider = bone.anchor.gameObject.AddComponent <CapsuleCollider>();

            capsuleCollider.enabled   = false;
            capsuleCollider.direction = direction;
            bone.collider             = capsuleCollider;

            Vector3 center = Vector3.zero;

            center[direction]      = distance / 2f;
            capsuleCollider.center = center;
            capsuleCollider.height = Mathf.Abs(distance);
            capsuleCollider.radius = Mathf.Abs(distance * bone.radiusScale);
        }