简体   繁体   English

在Unity3D中无法访问子对象的动画

[英]Can't Access the Animation of the Child Object in Unity3D

I have an object, let's say for example an ObjectBoy. 我有一个对象,例如说一个ObjectBoy。 My object walks forward after I click on the terrain, but it is walking with it's back first, and it looks like it's walking backwards. 单击地形后,我的对象向前走,但它先向后走,看起来好像向后走。 I've researched in Unity forums that to overcome this, you must put the object in an Empty game object as it's parent and correct the orientation on the child. 为了解决这个问题,我在Unity论坛中进行了研究,您必须将该对象作为它的父对象放入Empty游戏对象中,并更正子对象的方向。 Now, everything works fine except the animation is not playing anymore, it just float on the terrain to go to it's destination. 现在,一切正常,除了动画不再播放,它只是漂浮在地形上以到达目的地。 The solution I'm thinking to this was to access the animation in the child but I can't see anywhere if it's possible. 我正在考虑的解决方案是访问儿童中的动画,但如果有可能,我看不到任何地方。 Here's my code: 这是我的代码:

public class ClickToMove : MonoBehaviour {
    NavMeshAgent navAgent;
    Animation animation;
    public AnimationClip runAnimation;
    public AnimationClip idleAnimation;

    void Start ()
    {
        navAgent = GetComponent<NavMeshAgent>();
        animation = GetComponent<Animation>();
    }

    void Update ()
    {
        move();
        animate();
    }

    void move()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Input.GetMouseButton(0))
        {
            if (Physics.Raycast(ray, out hit, 100))
            {
                navAgent.SetDestination(hit.point);
            }
        }
    }

    void animate()
    {
        if (navAgent.velocity.magnitude > 0.5f)
        {
            animation.CrossFade(runAnimation.name);
        }
        else 
        {
            animation.CrossFade(idleAnimation.name);
        }
    }
}

you can get a component that is in children with GetComponentInChildren 您可以使用GetComponentInChildren获得位于子级中的GetComponentInChildren

public Animation animation;
    void Example() {
        animation= GetComponentInChildren<Animation>();
    }

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

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