简体   繁体   English

获取一个子对象以在Unity中复制父对象的动画

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

I'm trying to animate an object (a body) with multiple child components (clothes) that are designed to animate alongside the parent using identical animation controllers to the parent, but the code framework we're using is designed to only handle one animation controller and is too complex to rework to our needs in the timeframe we want it done. 我正在尝试对具有多个子组件(衣服)的对象(身体)进行动画处理,这些子组件旨在使用与父对象相同的动画控制器与父对象一起进行动画处理,但是我们正在使用的代码框架旨在仅处理一个动画控制器,而且过于复杂,无法在我们希望完成的时间范围内满足我们的需求。 The result is the torso runs, but the clothes remain static. 结果是躯干奔跑,但衣服保持静止。

The quick and dirty solution I'm trying to use is to use a script to read the parent's animation state and tell the child objects to copy that state, but I'm not sure how to go about this. 我尝试使用的快速而肮脏的解决方案是使用脚本读取父级的动画状态,并告诉子级对象复制该状态,但是我不确定该如何处理。 Does anyone know how I should do this? 有人知道我该怎么做吗? I've considered either using a script attached to the parent or one attached to each child object, but I'm not sure what the correct approach is. 我已经考虑过使用附加到父对象的脚本或附加到每个子对象的脚本,但是我不确定哪种方法正确。

I'm doing something similar, where I have a parent object which is a character in my game, and it has a bunch of child objects for its clothing, hair, etc. Each object's animation controllers have the same named parameters. 我正在做类似的事情,我有一个父对象,它是游戏中的角色,它的衣服,头发等都有很多子对象。每个对象的动画控制器具有相同的命名参数。

I have this script attached to the "parent" object (the character). 我将此脚本附加到“父”对象(字符)上。 Then instead of calling GetComponent<Animator>().SetInteger(...) on the parent, I call GetComponent<AgentAnimator>().SetInteger(...) , and that handles setting the parameters on the parent and its direct children. 然后,我不调用父级上的GetComponent<Animator>().SetInteger(...) ,而是调用GetComponent<AgentAnimator>().SetInteger(...) ,它处理父级及其直接子级上的参数设置。 。 If your parent object has grandchildren or deeper descendants, then you'll have to update this script to read further down the descendant tree (or use could attach AgentAnimator s to your children, and use the same scheme to deal with any level of children). 如果您的父对象具有孙子项或更深的子孙,那么您将必须更新此脚本以进一步读取子孙树(或使用可以将AgentAnimator附加到您的子孙,并使用相同的方案来处理任何级别的子孙) 。

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM