Hello,
I want to make a root motion script that takes ANY bone of a skeleton and performs root motion based on that bone. So each frame, the child bone's change in position is measured, subtracted from the root, and added to the SkeletonAnimation's GameObject.
This would make animating for root motion much easier. Things like feet would hold still if they're direct descendants of the root, and it would be easy to make the player's environment collider stay in a place that makes sense, like player's center of mass (just put a bone in your skeleton's hierarchy so that it remains at the center of mass and use that for root motion).
I've managed to half-way do this with the following code, assuming the root of the skeleton is stationary within the animation data (I call the bone that's used for root motion 'fulcrum'):
protected virtual void FixedUpdate ()
{// FixedUpdate is called once per fixed framerate frame
fulcrumXPosDelta = fulcrumTransform.position.x - root.transform.position.x - prevFulcrumXPos;
if (skeletonAnimation.Skeleton.FlipX)
{
root.transform.Translate(fulcrumXPosDelta, 0, 0);
goTransform.Translate(fulcrumXPosDelta, 0, 0);
}
else
{
root.transform.Translate(-fulcrumXPosDelta, 0, 0);
goTransform.Translate(fulcrumXPosDelta, 0, 0);
}
prevFulcrumXPos = fulcrumTransform.position.x - root.transform.position.x;
}
I've since found Pharan's root motion script and got it working (had to put it in spine/runtime/spine-unity/components and had to change flipX/Y to FlipX/Y).
I'd like to adapt Pharan's SpineRootMotion to work the same way, because his seems much more robust in terms of anticipating issues, but I can't figure out how to do it.
Thanks for any help you can provide.
Extra Credit: Why do I have to have the if (skeletonAnimation.Skeleton.FlipX)
in that code? Is it something to do with the way Spine-Unity implements ScaleX/Y? I just figured out that that's what it needed because of how the animation looked without it.