简体   繁体   English

不能在一个对象Unity3D上播放3个动画

[英]Can't Play 3 Animations on one Object Unity3D

I want to play 3 animations on a cube, when a condition is checked the first animation should be played, then when the second condition is checked, the second animation, and the same thing for the last one. 我想在一个多维数据集上播放3个动画,当一个条件被选中时,应该播放第一个动画,然后在第二个条件被选中时,播放第二个动画,最后一个播放相同的东西。

So I've created 3 animations and make them as legacy, then I attached animation component to the cube, and add the animations to it. 因此,我创建了3个动画并将它们作为旧版,然后将动画组件附加到多维数据集,然后将动画添加到其中。 The problem I have with the script below is that the first animation works fine, but the 2 rest didn't. 我在下面的脚本中遇到的问题是,第一个动画可以正常工作,而其余两个动画却不能。 What should I do to fix that? 我该怎么做才能解决这个问题?

Animation CubeRot;
    bool Rot = false;

    // Use this for initialization
    void Start () {

        CubeRot = gameObject.GetComponent<Animation>();
    }

    // Update is called once per frame
    void Update () {

        if (FindObjectOfType<HoseController>().Rotated1 == true)
        {
            if (!Rot)
            {
                CubeRot.Play("Rot1");
                Rot = true;
            }
        }
        if (FindObjectOfType<HoseController>().Rotated2 == true)
        {
            if (!Rot)
            {
                CubeRot.Play("Rot2");
                Rot = true;
            }
        }
    }

Is it because you are setting Rot = true; 是因为您将Rot = true;设置Rot = true;Rot = true; in the first update. 在第一次更新中。 Then the second update requires the Rot variable to be false but its still true from the 1st update? 然后第二次更新要求Rot变量为false但是从第一次更新起它仍然为true?

First: You require Rot to be false in both if. 第一:您都要求Rot在两个if中都为false。

Second: If Rotated1 is true the first part will loop (since you are in the Update) and since you do not set it to false in your if it will continue to loop. 第二:如果Rotated1为true,则第一部分将循环(因为您处于Update中),并且由于未将其设置为false,因此它将继续循环。 You can even avoid to use both Rotated1 and Rot 您甚至可以避免同时使用Rotated1和Rot

Also you should try to avoid the legacy animation component. 另外,您应该尝试避免使用旧版动画组件。 It should be easy to do the same behaviour in the new Animator component 在新的Animator组件中执行相同的行为应该很容易

Finally avoid to use FindObjectOfType each time 最后避免每次都使用FindObjectOfType

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

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