布尔值设置为真后让动画播放?

Make Animation Play After Boolean is Set to True?

我Unity的使用还是比较流利的,但是关于Mechanim和Animations我目前还不是很好,所以请不要给我太多的困难哈哈。所以我的 GameManager 脚本中有这个布尔值:

public bool countDownDone = false;

一旦我的“3,2,1,开始!”,这个布尔值就被设置为真。倒数计时器在我的游戏开始时结束。在该布尔值设置为 true 之后,我游戏中的所有内容都开始了。示例:

using UnityEngine;
using System.Collections;

public class PlaneMover : MonoBehaviour {

    private GameManagerScript GMS; // Reference to my GameManager Script

    public float scrollSpeed;
    public float tileSizeZAxis;
    public float acceleration; //This has to be a negative number in the inspector. Since our plane is coming down.

    private Vector3 startPosition;

    void Start () {

        GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript> (); //Finds the Script at the first frame

        // Transform position of the quad
        startPosition = transform.position;
    }

    void Update () {

        if (GMS.countDownDone == true) //Everything starts once the countdown ends. 
        {

            /* Every frame - time in the game times the assigned scroll speed 
                and never exceed the length of the tile that we assign */
            float newPosition = Mathf.Repeat (Time.time * scrollSpeed, tileSizeZAxis);

            // New position equal to the start position plus vector3forward times new position
            transform.position = startPosition + Vector3.forward * newPosition; // was vector3.forward

            scrollSpeed += Time.deltaTime * acceleration; // This makes the plane increase in speed over time with
                                                          // whatever our acceleration is set to.

        }
    }
}

我有这个爬行动画,它在游戏一开始就播放(甚至在计时器结束之前)并且永远循环播放。我的问题是,一旦布尔值设置为 "true",我如何使爬行动画也开始?或者我只是将它应用到 CoRoutine 并让它在 3 秒后播放?我对是引用 Animation 还是 Animator 进行了广泛的研究,对此我也感到困惑。有什么建议吗?如果您需要有关我的问题的更多图片或详细信息,请告诉我。谢谢! :)

下面是新代码

using UnityEngine;
using System.Collections;

public class Crawling : MonoBehaviour {

    Animator animator;

    private GameManagerScript GMS;

    void Start () 
    {
        animator = GetComponent<Animator> ();

        GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript> ();

    }

    void Update ()
    {
        if (GMS.countDownDone == true) 
        {
            animator.Play("Main Character Crawling", 1);
        }
    }   
}

I have this Crawling animation that plays at the very beginning of the game (Even before the Timer ends) and loops forever. My question is , how do I make that crawling animation also start once that boolean is set to "true"?

解决方案是从脚本播放动画。 删除当前Animation.

Select 您的 PlaneMover 脚本附加到的游戏对象,将动画和 Animator 组件附加到它。确保未选中动画的 Play Automatically

public class PlaneMover : MonoBehaviour {
private GameManagerScript GMS; // Reference to my GameManager Script

    public float scrollSpeed;
    public float tileSizeZAxis;
    public float acceleration; //This has to be a negative number in the inspector. Since our plane is coming down.

    private Vector3 startPosition;

    Animation animation;
    public AnimationClip animationClip; //Assign from Editor

    void Start () {

        GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript> (); //Finds the Script at the first frame

        // Transform position of the quad
        startPosition = transform.position;

        animation = GetComponent<Animation>();

        //Add crawing Animation
        animation.AddClip(animationClip, "Crawling");
        //Add other animation clips here too if there are otheres
    }

    void Update () 
    {

        if (GMS.countDownDone) //Everything starts once the countdown ends. 
        {

            /* Every frame - time in the game times the assigned scroll speed 
                and never exceed the length of the tile that we assign */
            float newPosition = Mathf.Repeat (Time.time * scrollSpeed, tileSizeZAxis);

            // New position equal to the start position plus vector3forward times new position
            transform.position = startPosition + Vector3.forward * newPosition; // was vector3.forward

            scrollSpeed += Time.deltaTime * acceleration; // This makes the plane increase in speed over time with
                                                          // whatever our acceleration is set to.

            //Play Animation
            animation.PlayQueued("Crawling", QueueMode.CompleteOthers);
        }
    }
} }

我解决了问题!! 这是我的脚本:

using UnityEngine;
using System.Collections;

public class Crawling : MonoBehaviour {

    public Animator animator;

    private GameManagerScript GMS;

    void Start () 
    {
        animator = GetComponent<Animator> ();

        GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript> ();

    }

    void Update ()
    {
        if (GMS.countDownDone == true) {
            animator.enabled = true;
        } 
        else 
        {
            animator.enabled = false;
        }
    }   
}

我所做的只是在 "countDownDone" 变为 "true" 时启用我在检查器中附加的动画器,为了增加安全性,我添加了 "else" 以禁用它.如果有人注意到我可以改进此脚本的方法,请告诉我。谢谢:)

较短的版本:

if (GMS.countDownDone) 
 animator.enabled = true; 
 else 
animator.enabled = false;