Hello! If you are using Spine PRO (which is required for being able to define IK Constraints in Spine Editor), then the animator/rigger needs to setup an IK Constraint.
Then at runtime, all you need to do is to control the IK Target bone position. The Spine-Unity runtime provides a method that allows you to set a bone position according to a given Unity world position:
using UnityEngine;
using Spine.Unity;
public class BoneControllerSample : MonoBehaviour {
[SpineBone]
public string boneName;
Spine.Bone bone;
public Vector3 targetPosition;
void Start () {
SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
this.bone = skeletonAnimation.Skeleton.FindBone(boneName);
skeletonAnimation.UpdateLocal += SkeletonAnimation_UpdateLocal;
}
void SkeletonAnimation_UpdateLocal (ISkeletonAnimation animated) {
var localPositon = transform.InverseTransformPoint(targetPosition);
bone.SetPosition(localPositon);
}
}
If you are using Spine Essential, you can't set up an IK Constraint. So you'd need to do the math yourself. If that's the case, having a simpler skeleton (like in the video you linked) will make things easier.
using UnityEngine;
using Spine.Unity;
public class BoneControllerSample : MonoBehaviour {
[SpineBone]
public string boneName;
Spine.Bone bone;
public Vector3 targetPosition;
void Start () {
SkeletonAnimation skeletonAnimation = GetComponent<SkeletonAnimation>();
this.bone = skeletonAnimation.Skeleton.FindBone(boneName);
skeletonAnimation.UpdateWorld += SkeletonAnimation_UpdateWorld;
}
void SkeletonAnimation_UpdateWorld (ISkeletonAnimation animated) {
var bonePosition = bone.GetWorldPosition(this.transform);
var direction = targetPosition - bonePosition;
float rotation = DirectionToRotation(direction, this.transform);
float parentRotation = bone.parent.WorldRotationX;
bone.RotateWorld(rotation - parentRotation);
}
static float DirectionToRotation (Vector3 direction, Transform transform) {
var localDirection = transform.InverseTransformDirection(direction);
return Mathf.Atan2(localDirection.y, localDirection.x) * Mathf.Rad2Deg;
}
}