public void AutoSetup() { if (AutoSetupEndJoint == null) { return; } var joints = new List <Transform>(); joints = AddIntermediateObjects(joints); if (EndOffset) { joints = AddEndOffset(joints); } var ikParent = GameObject.Find("_IkControllers"); if (ikParent == null) { ikParent = new GameObject { name = "_IkControllers" } } ; Chain = new RageIkChain(joints, ikParent.transform, TwoDmode); Chain.Init(); Chain.Target = CreateController(ikParent, joints[joints.Count - 1].gameObject, "IkTarget_" + gameObject.name).transform; SetupJointLimiters(Chain); }
public static void Solve(RageIkChain chain) { if (chain.Joints.Count < 2) { return; } chain.Init(); if (IsTargetUnreachable(chain)) { for (int i = 0; i < chain.Joints.Count - 1; i++) { Quaternion rotation = Quaternion.FromToRotation(chain.Joints[i + 1].position - chain.Joints[i].position, chain.Target.position - chain.Joints[i].position); AddRotation(chain.Joints[i], rotation, chain); var limiter = chain.Joints[i].GetComponent <RageIkJointLimiter>(); if (limiter == null || !limiter.Live) { continue; } if (limiter.ValidVector(limiter.RestDirection)) { continue; } if (Vector3.Angle(limiter.RestDirection, limiter.MaxAngle) > Vector3.Angle(limiter.RestDirection, limiter.MinAngle)) { AddRotation(chain.Joints[i], Quaternion.FromToRotation(limiter.RestDirection, limiter.MinAngle), chain); continue; } AddRotation(chain.Joints[i], Quaternion.FromToRotation(limiter.RestDirection, limiter.MaxAngle), chain); } ChangeLastElementRotation(chain); return; } int tries = 0; Vector3 rootInitial = chain.Joints[0].position; float targetDelta = Vector3.Distance(chain.Joints[chain.Joints.Count - 1].position, chain.Target.position); var desiredPositions = new Vector3[chain.Joints.Count]; for (int i = 0; i < chain.Joints.Count; i++) { desiredPositions[i] = chain.Joints[i].position; } while (targetDelta > Accuracy && tries < MaxIterations) { ForwardReachingPhase(chain, desiredPositions); BackwardReachingPhase(chain, desiredPositions, rootInitial); targetDelta = Vector3.Distance(desiredPositions[chain.Joints.Count - 1], chain.Target.position); tries++; } }