I updated this script to be a bit cleaner and have better comments and stuff. I also cut everything that wasn't specific to just manipulating IK at runtime. You can change the .Mix at runtime to fade the IK in and out, but I've found that a better solution in Unity is to have an animation with 1 key frame with 100% mix in Spine, and nothing else. Then, put that animation on its own layer by itself in the mecanim animator. Then!~ manipulate the layer weight at runtime with Animator.SetLayerWeight. It worked WAY better!
using UnityEngine;
namespace Spine.Unity {
public class IKTracking : MonoBehaviour
{
#region Variables
[Header("Tracking Variables")]
[SpineBone]
public string boneToMove;
public Transform transformToFollow;
Spine.Bone targetBone;
#endregion
void Start()
{
//getting reference to the ik target bone from the skeleton
SkeletonAnimator skeletonAnimator = GetComponent<SkeletonAnimator>();
targetBone = skeletonAnimator.Skeleton.FindBone(boneToMove);
//running function to update ik target bone position and get the skeleton to follow it
skeletonAnimator.UpdateLocal += SkeletonAnimator_UpdateLocal;
}
#region Functions
//setting ik's target bone to follow the transform that you wanted & animating the skeleton to follow dat ik
void SkeletonAnimator_UpdateLocal(ISkeletonAnimation animated)
{
Vector3 localPositon = transform.InverseTransformPoint(transformToFollow.transform.position);
targetBone.SetPosition(localPositon);
}
#endregion
}
}