• Runtimes
  • Move a Bone during runtime

  • Изменено
Related Discussions
...

Hello there,

Attached image is a screenshot of my Spine project window.
I am trying to make my player fist aim at my mouse direction during runtime in Unity.
By using FistAimL and FistAimR, this has no issue but will need a lot of calculations. Moreover, since FistAimR also controls part of the character's body rotation, I want to be able to separate the body rotation and the fist aiming.
As a result, I tried to move the LeftFist and RightFist components so that the fist aiming won't need much calculation, and this way, I can separate the body rotation, left fist aim, and right fist aim to 3 different control.
However, when trying to do that in Unity runtime, the RightFist and LeftFist seem locked. I used debug.log to check if the coordinates actually moved. The result is the coordinate actually changes, but the character stays froze.
Below is my code:

public void AimLeftFist()
{
Vector2 aim = new Vector2(
mousePosition.x,
mousePosition.y);
fistAimL.SetLocalPosition(aim);
fistAimL.Skeleton.UpdateWorldTransform();
Debug.Log(fistAimL.X + ", " + fistAimL.Y);
fistAimL.Update();
}

mousePosition is relative mouse position to player transform.
fistAimL is the bone "RightFist"
I have tried adding UpdateWorldTransform or Update to fistAimL and fistAimL.Skeleton and fistAimL.Parent.Skeleton and none of them works.
This function is placed in LateUpdate too.
Can someone please help? Thanks!

Have you perhaps keyed the location of your fistAimL bone in any of your currently playing animations? If so, then you should be able to solve the problem easily by deleting the keys which overwrite your fistAimL bone position when the animation is being applied each frame, overwriting your changes if you get the update order wrong (applying your changes before the animation each frame).

If you need to keep the keys of the fistAimL bone location (for whatever reason), you need to apply your changes after the animation has positioned your bone. Since your code is not showing from where you are calling your AimLeftFist() method, I assume that you call it in Update. While changing the script execution order of your component script, it's usually best to use the skeletonAnimation.UpdateLocal callback delegate to call your method, as described here on the spine-unity documentation pages:
spine-unity Runtime Documentation: Life cycle

Thanks Harald,

I created a separate Bone and placed RightFist and LeftFist as the child of that new Bone. Then I move this bone in unity. This somehow works for now. I will definitely try skeletonAnimation.UpdateLocal later.

Glad it helped, thanks for getting back to us.