• Runtimes
  • 关于根骨骼运动相关疑问

和4.2版本更新的例子一样,我做了一个僵尸速度不规则移动的spine动画,导入unity后我该如何设置播放动画时,让动画跟随我用于记录位移的Bone:"roleroot",以及避免一些其他可能的问题,例如循环播放动画时,该动画从最后一帧到第一帧可能会回到初始坐标,还有我的roleroot节点所在的位置是否需要有要求例如我现在的这个节点是在僵尸的头部位置

Related Discussions
...

在Unity中使用Spine动画时,如果你想让动画跟随一个特定的骨骼(例如你的"roleroot"骨骼),并且避免循环播放时位置跳跃的问题,可以考虑以下步骤:

  1. 使用Root Motion

    • 在Spine中,你可以通过动画根骨骼(root bone)来实现根运动(root motion)。在Unity中,你可以使用Spine-Unity运行时的SkeletonRootMotion组件来提取和应用根运动。
    • 确保你的roleroot骨骼在Spine中设置为根骨骼,并且动画中所有的位移都应用在这个骨骼上。
  2. 设置SkeletonRootMotion组件

    • 在Unity中,选择你的Spine动画对象,添加SkeletonRootMotion组件。
    • 配置SkeletonRootMotion组件,使其跟随你的roleroot骨骼。你可以在组件的设置中选择要跟随的骨骼。
  3. 避免循环播放位置跳跃

    • 确保你的动画在Spine中是无缝循环的。如果动画的最后一帧和第一帧之间有位置跳跃,可以在Spine中调整动画,使其平滑过渡。
    • 在Unity中,你可以通过编写脚本来检测动画的播放状态,并在动画结束时手动调整位置,以避免跳跃。
  4. 骨骼位置要求

    • roleroot骨骼的位置应该在角色的根部(例如脚部或腰部),而不是头部。这样可以确保根运动的位移是合理的,并且不会导致角色在移动时出现不自然的效果。

以下是一个简单的示例代码,展示如何在Unity中使用SkeletonRootMotion组件:

using Spine.Unity;
using UnityEngine;

public class ZombieController : MonoBehaviour
{
    public SkeletonAnimation skeletonAnimation;
    public string rootBoneName = "roleroot";

    void Start()
    {
        // 获取SkeletonRootMotion组件
        var rootMotion = skeletonAnimation.GetComponent<SkeletonRootMotion>();
        if (rootMotion == null)
        {
            rootMotion = skeletonAnimation.gameObject.AddComponent<SkeletonRootMotion>();
        }

        // 设置要跟随的根骨骼
        rootMotion.rootMotionBoneName = rootBoneName;
    }

    void Update()
    {
        // 检测动画播放状态,避免位置跳跃
        if (skeletonAnimation.AnimationState.GetCurrent(0).IsComplete)
        {
            // 手动调整位置或其他处理逻辑
        }
    }
}

通过以上步骤和代码示例,你可以在Unity中实现根运动,并避免循环播放时的位置跳跃问题。

    Spinebot 我按照第二步配置后为什么只在动画播放结束后才更新一次位置,我希望每一帧都和动画同步

      fengmin I am not sure about updating the position only once after the animation plays. Could you please show us what is going on with a test project or something?
      For your information, the following video demonstrates what to expect when using the SkeletonRootMotion component:

      (In this example, the walk and run animations are combined into a single animation for testing purposes, and the root bone is moved in the animation.)

      You can see in the upper right corner of this video that the GameObject's PositionX value is moving in the positive direction while the SkeletonRootMotion is being applied.

      The settings for the SkeletonRootMotion component in this video are here:

        Misaki 我留意到该脚本未激活时,角色能在unity中移动,是动画直接更改了root节点位置吗,我的动画root节点没有动,我只是用了一个叫做roleroot的节点去记录了XY位移信息然后在SkeletonRootMotion中选择的roleroot节点

        @fengmin Unfortunately the machine translation of your message was a bit ambiguous. If the following explanation does not help, please describe your setup and the problems you encountered in more detail.

        The SkeletonRootMotion component does two things:
        1) It sets the root-motion bone's position movement to zero (to turn forward movement into in-place movement), and
        2) It moves the GameObject by the root-motion bone's movement that happened this frame (to add forward movement again, but at the GameObject).

        As a consequence you need to have e.g. the character's hip as a child of the root-motion bone and then move the root-motion bone forward during a walk cycle:

        root
        - rootmotion // move this bone forward
        -- hip

        If you have the root-motion bone incorrectly set up "parallel" to your actual skeleton like this:

        root
        - hip             // you move this bone forward
        - rootmotion      // you also move this bone forward

        step (1) setting the root-motion bone movement to zero won't affect your actual skeleton. It's not turning your movement into in-place movement. Thus you would end up with double-forward movement.

        4 months later

        Spinebot
        所以 spine 的根骨骼运动是需要再 spine 软件里进行运动设置,然后导入到 unity 中实际上还需在动画播放完成后脚本手动调整当前位置以避免闪回?或者运动回退?
        所以他原理其实就是在播放动画,但是仍然需要开发者手动设定当前位置来实现正常的走路移动是吧?
        感觉有点复杂,会导致实际用户搞不清楚到底怎么使用,文档也没说明白

        So the root bone movement in Spine needs to be set up in the Spine software first, and then when imported into Unity, the script still requires manual adjustment of the position after the animation playback is completed to avoid snapping back or any backward movement?
        
        So the principle is essentially that while playing the animation, developers still need to manually set the position to achieve normal walking movement, right? It feels a bit complicated and could lead to actual users being confused about how to use it, as the documentation doesn't explain it clearly.

          dyingstranded So the principle is essentially that while playing the animation, developers still need to manually set the position to achieve normal walking movement, right? It feels a bit complicated and could lead to actual users being confused about how to use it, as the documentation doesn't explain it clearly.

          No, you never have to manually set any position. You have to add a SkeletonRootMotion component and set up the component accordingly, i.e. select the target root-motion bone. If you don't see root-motion behave as expected, likely there is something wrong with your setup in the Spine project.

          Also make sure you're using the latest spine-unity runtime to include the latest bugfixes.
          https://esotericsoftware.com/spine-unity-download

            24 days later