获取一个childobject复制Unity中parent的动画

Get a child object to copy the animation of the parent in Unity

我正在尝试使用多个 child 组件(衣服)为 object(一个 body)制作动画,这些组件旨在与 parent 一起使用相同的动画parent,但我们使用的代码框架旨在仅处理一个动画控制器,并且过于复杂,无法在我们希望完成的时间范围内重新设计以满足我们的需求。结果是躯干跑了,衣服还是不动。

我尝试使用的快速而肮脏的解决方案是使用脚本读取 parent 的动画状态并告诉 child object 复制它状态,但我不确定该怎么做。有谁知道我应该怎么做?我考虑过使用附加到 parent 的脚本或附加到每个 child object 的脚本,但我不确定正确的方法是什么。

我正在做类似的事情,我有一个 parent object 这是我游戏中的一个角色,它有一堆 child object s 用于衣服、头发等。每个 object 的动画控制器具有相同的命名参数。

我将此脚本附加到 "parent" object(角色)。然后,我没有在 parent 上调用 GetComponent<Animator>().SetInteger(...),而是调用 GetComponent<AgentAnimator>().SetInteger(...),它负责在 parent 及其直接 children 上设置参数。如果您的 parent object 有 grandchildren 或更深的后代,那么您必须更新此脚本以进一步阅读后代树(或使用 could attach AgentAnimator s到你的children,用同样的方案来处理任何级别的children)。

public class AgentAnimator : MonoBehaviour
{
    public void SetInteger(string id, int value)
    {
        Animator animator = GetComponent<Animator>();
        animator.SetInteger(id, value);
        var childComponents = GetComponentsInChildren<Animator>();
        foreach (var child in childComponents)
        {
           child.SetInteger(id, value);
        }
    }

    // Do the same for SetBool, SetTrigger, etc.
}