コード例 #1
0
        void LateUpdate()
        {
            // If target has changed...
            if (target != lastTarget)
            {
                if (lastTarget == null && target != null && ik.solver.IKPositionWeight <= 0f)
                {
                    lastPosition         = target.position;
                    dir                  = target.position - pivot;
                    ik.solver.IKPosition = target.position + offset;
                }
                else
                {
                    lastPosition = ik.solver.IKPosition;
                    dir          = ik.solver.IKPosition - pivot;
                }

                switchWeight = 0f;
                lastTarget   = target;
            }

            // Smooth weight
            float targetWeight = target != null ? weight : 0f;

            ik.solver.IKPositionWeight = Mathf.SmoothDamp(ik.solver.IKPositionWeight, targetWeight, ref weightV, weightSmoothTime);
            if (ik.solver.IKPositionWeight >= 0.999f && targetWeight > ik.solver.IKPositionWeight)
            {
                ik.solver.IKPositionWeight = 1f;
            }
            if (ik.solver.IKPositionWeight <= 0.001f && targetWeight < ik.solver.IKPositionWeight)
            {
                ik.solver.IKPositionWeight = 0f;
            }

            if (ik.solver.IKPositionWeight <= 0f)
            {
                return;
            }

            // Smooth target switching
            switchWeight = Mathf.SmoothDamp(switchWeight, 1f, ref switchWeightV, targetSwitchSmoothTime);
            if (switchWeight >= 0.999f)
            {
                switchWeight = 1f;
            }

            if (target != null)
            {
                ik.solver.IKPosition = Vector3.Lerp(lastPosition, target.position + offset, switchWeight);
            }

            // Smooth turn towards target
            if (smoothTurnTowardsTarget != lastSmoothTowardsTarget)
            {
                dir = ik.solver.IKPosition - pivot;
                lastSmoothTowardsTarget = smoothTurnTowardsTarget;
            }

            if (smoothTurnTowardsTarget)
            {
                Vector3 targetDir = ik.solver.IKPosition - pivot;

                // Slerp
                if (slerpSpeed > 0f)
                {
                    dir = Vector3.Slerp(dir, targetDir, Time.deltaTime * slerpSpeed);
                }

                // RotateTowards
                if (maxRadiansDelta > 0 || maxMagnitudeDelta > 0f)
                {
                    dir = Vector3.RotateTowards(dir, targetDir, Time.deltaTime * maxRadiansDelta, maxMagnitudeDelta);
                }

                // SmoothDamp
                if (smoothDampTime > 0f)
                {
                    float yaw       = V3Tools.GetYaw(dir);
                    float targetYaw = V3Tools.GetYaw(targetDir);
                    float y         = Mathf.SmoothDampAngle(yaw, targetYaw, ref yawV, smoothDampTime);

                    float pitch       = V3Tools.GetPitch(dir);
                    float targetPitch = V3Tools.GetPitch(targetDir);
                    float p           = Mathf.SmoothDampAngle(pitch, targetPitch, ref pitchV, smoothDampTime);

                    float dirMag = Mathf.SmoothDamp(dir.magnitude, targetDir.magnitude, ref dirMagV, smoothDampTime);

                    dir = Quaternion.Euler(p, y, 0f) * Vector3.forward * dirMag;
                }

                ik.solver.IKPosition = pivot + dir;
            }

            // Min distance from the pivot
            ApplyMinDistance();

            // Root rotation
            RootRotation();

            // Offset mode
            if (useAnimatedAimDirection)
            {
                ik.solver.axis = ik.solver.transform.InverseTransformVector(ik.transform.rotation * animatedAimDirection);
            }
        }