InverseKinematics.resolveSK2D C# (CSharp) Method

resolveSK2D() public method

public resolveSK2D ( ) : void
return void
    public void resolveSK2D()
    {
        for (int it = 0; it < iterations; it++) {
            int i = ChainLength;
            Transform bone = transform;
            Bone b = bone.GetComponent<Bone>();

            while (--i >= 0 && bone != null) {
                Vector3 root = bone.position;

                // Z position can be different than 0
                Vector3 root2tip = ((Vector3)b.Head - (Vector3)(Vector2)root);
                Vector3 root2target = (((target != null) ? (Vector3)(Vector2)target.transform.position : (Vector3)b.Head) - (Vector3)(Vector2)root);

                // Calculate how much we should rotate to get to the target
                float angle = SignedAngle(root2tip, root2target, bone);

                // If you want to flip the bone on the y axis invert the angle
                float yAngle = Utils.ClampAngle(bone.rotation.eulerAngles.y);

                // If the skeleton is rotated then make sure the angle is modified accordingly
                int yModifier = (_skeleton && _skeleton.transform.localRotation.eulerAngles.y == 180.0f
                                && _skeleton.transform.localRotation.eulerAngles.x == 0.0f) ? 1 : -1;

                if (yAngle > 90 && yAngle < 270)
                angle *= yModifier;

                // "Slows" down the IK solving
                angle *= damping;

                // Wanted angle for rotation
                angle = -(angle - bone.localRotation.eulerAngles.z);

                if(nodeCache != null && nodeCache.ContainsKey(bone))
                {
                    // Clamp angle in local space
                    var node = nodeCache[bone];
                    angle = ClampAngle(angle, node.from, node.to);
                }

                Quaternion newRotation = Quaternion.Euler(bone.localRotation.eulerAngles.x, bone.localRotation.eulerAngles.y, angle);

                if (!IsNaNRot(newRotation)) {
                    bone.localRotation = newRotation;
                }

                bone = bone.parent;
            }
        }
    }

Usage Example

Exemplo n.º 1
0
    private void EditorUpdate()
    {
        foreach (Bone b in gameObject.GetComponentsInChildren <Bone>())
        {
            InverseKinematics ik = b.GetComponent <InverseKinematics>();

            if (ik != null && !editMode && ik.enabled && ik.influence > 0)
            {
                ik.resolveSK2D();
            }
        }
    }
All Usage Examples Of InverseKinematics::resolveSK2D