using UnityEngine.Scripting.APIUpdating; namespace UnityEngine.U2D.IK { /// /// Utility for 2D Limb IK Solver. /// [MovedFrom("UnityEngine.Experimental.U2D.IK")] public static class Limb { /// /// Solve based on Limb IK /// /// Target position. /// Length of the chains. /// Chain positions. /// Output angles for the chain's position. /// Always returns true. public static bool Solve(Vector3 targetPosition, float[] lengths, Vector3[] positions, ref float[] outAngles) { outAngles[0] = 0f; outAngles[1] = 0f; if (lengths[0] == 0f || lengths[1] == 0f) return false; var startToEnd = targetPosition - positions[0]; var distanceMagnitude = startToEnd.magnitude; var sqrDistance = startToEnd.sqrMagnitude; var sqrParentLength = (lengths[0] * lengths[0]); var sqrTargetLength = (lengths[1] * lengths[1]); var angle0Cos = (sqrDistance + sqrParentLength - sqrTargetLength) / (2f * lengths[0] * distanceMagnitude); var angle1Cos = (sqrDistance - sqrParentLength - sqrTargetLength) / (2f * lengths[0] * lengths[1]); if (angle0Cos >= -1f && angle0Cos <= 1f && angle1Cos >= -1f && angle1Cos <= 1f) { outAngles[0] = Mathf.Acos(angle0Cos) * Mathf.Rad2Deg; outAngles[1] = Mathf.Acos(angle1Cos) * Mathf.Rad2Deg; } return true; } } }