示例#1
0
    private Vector3 GetBendNormalStrategy(BendNormalStrategy bendNormalStrategy, Vector3 actualTargetPos)
    {
        Vector3 bendNormal = Vector3.zero;

        switch (bendNormalStrategy)
        {
        case BendNormalStrategy.followTarget:
            bendNormal = -Vector3.Cross(actualTargetPos - bone1.trans.position, target.forward);
            break;

        case BendNormalStrategy.rightArm:
            bendNormal = Vector3.down;
            break;

        case BendNormalStrategy.leftArm:
            bendNormal = Vector3.up;
            break;

        case BendNormalStrategy.spine:
            bendNormal = bone1.GetBendNormalFromCurrentRotation();
            break;

        default:
            break;
        }
        return(bendNormal);
    }
示例#2
0
    public void UpdateIK()
    {
        //clamp target if distance to target is longer than bones combined
        Vector3 actualTargetPos;
        float   overallLength = Vector3.Distance(bone1.trans.position, target.position);

        if (overallLength > bone1.length + bone2.length)
        {
            actualTargetPos = bone1.trans.position + (target.position - bone1.trans.position).normalized * (bone1.length + bone2.length);
            overallLength   = bone1.length + bone2.length;
        }
        else
        {
            actualTargetPos = target.position;
        }

        //calculate bend normal
        //you may need to change this based on the model you chose
        Vector3 bendNormal = GetBendNormalStrategy(bendNormalStrategy, actualTargetPos);

        //calculate bone1, bone2 rotation
        Vector3 bendDirection = GetBendDirection(actualTargetPos, bendNormal);

        if (IKBone4 != null)
        {
            bone4.trans.rotation = bone4.GetRotation(bendDirection, bendNormal);
        }

        if (trackSecondBone && secondTarget != null && bendNormalStrategy != BendNormalStrategy.spine)
        {
            Vector3 secondActualTargetPos;
            float   secondOverallLength = Vector3.Distance(bone1.trans.position, secondTarget.position);
            if (secondOverallLength > bone1.length)
            {
                secondActualTargetPos = bone1.trans.position + (secondTarget.position - bone1.trans.position).normalized * bone1.length;
                secondOverallLength   = bone1.length;
            }
            else
            {
                secondActualTargetPos = secondTarget.position;
            }

            Vector3 secondbendNormal = GetBendNormalStrategy(bendNormalStrategy, secondActualTargetPos);
            //calculate bone2 rotation
            Vector3 secondbendDirection = GetSecondBoneBendDirection(secondActualTargetPos, secondbendNormal);

            // Rotating Shoulder/Thigh
            bone1.trans.rotation = bone1.GetRotation(secondbendDirection, secondbendNormal);
            //Set Elbow/Knee position
            bone2.trans.position = secondActualTargetPos;
            // bone2.trans.rotation = secondTarget.rotation;
            //Set Hand/Foot position
            bone3.trans.position = actualTargetPos;

            bone2.trans.rotation = bone2.GetRotation(actualTargetPos - bone2.trans.position, bone2.GetBendNormalFromCurrentRotation(bendNormal));
        }
        else
        {
            // Rotating bone1
            if (bendNormalStrategy == BendNormalStrategy.leftArm || bendNormalStrategy == BendNormalStrategy.rightArm)
            {
                bone1.trans.rotation = bone1.GetRotation(bendDirection, bendNormal) * Quaternion.Euler(-45, 0, 0);
            }
            else
            {
                bone1.trans.rotation = bone1.GetRotation(bendDirection, bendNormal);
            }

            // Rotating bone 2
            bone2.trans.rotation = bone2.GetRotation(actualTargetPos - bone2.trans.position, bone2.GetBendNormalFromCurrentRotation(defaultBendNormal));
        }
        bone3.trans.rotation = target.rotation;
        bone3.trans.position = actualTargetPos;
    }
示例#3
0
    public void UpdateIK()
    {
        //clamp target if distance to target is longer than bones combined
        Vector3 actualTargetPos;
        float   overallLength = Vector3.Distance(bone1.trans.position, target.position);

        if (overallLength > bone1.length + bone2.length)
        {
            actualTargetPos = bone1.trans.position + (target.position - bone1.trans.position).normalized * (bone1.length + bone2.length);
            overallLength   = bone1.length + bone2.length;
        }
        else
        {
            actualTargetPos = target.position;
        }

        //calculate bend normal
        //you may need to change this based on the model you chose
        Vector3 bendNormal = Vector3.zero;

        switch (bendNormalStrategy)
        {
        case BendNormalStrategy.followTarget:
            bendNormal = -Vector3.Cross(actualTargetPos - bone1.trans.position, target.forward);
            break;

        case BendNormalStrategy.rightArm:
            bendNormal = Vector3.down;
            break;

        case BendNormalStrategy.leftArm:
            bendNormal = Vector3.up;
            break;

        case BendNormalStrategy.head:
            bendNormal = bone1.GetBendNormalFromCurrentRotation();
            break;

        //case BendNormalStrategy.rightFoot:
        //    bendNormal = -Vector3.Cross(actualTargetPos - bone1.trans.position, target.forward);
        //    break;
        //case BendNormalStrategy.leftFoot:
        //    bendNormal = -Vector3.Cross(actualTargetPos - bone1.trans.position, target.forward);
        //    break;
        default:
            Debug.LogError("Undefined bendnormal strategy: " + bendNormalStrategy);
            break;
        }

        //calculate bone1, bone2 rotation
        Vector3 bendDirection = GetBendDirection(actualTargetPos, bendNormal);

        // Rotating bone1
        bone1.trans.rotation = bone1.GetRotation(bendDirection, bendNormal);

        // Rotating bone 2
        bone2.trans.rotation = bone2.GetRotation(actualTargetPos - bone2.trans.position, bone2.GetBendNormalFromCurrentRotation(defaultBendNormal));
        //bone2.trans.rotation = bone2.GetRotation(actualTargetPos - bone2.trans.position, Quaternion.AngleAxis(debugAngle, target.forward)* target.up);

        bone3.trans.rotation = target.rotation;
    }