在布尔设置为True后进行动画播放?

时间:2016-06-13 12:24:28

标签: c# animation unity3d boolean unity5

我使用Unity非常流利,但对于Mechanim和Animations,我目前不太好,所以请不要给我太多的困难时间哈哈。所以我在我的GameManager脚本中有这个布尔值:

public bool countDownDone = false;

一旦我的“3,2,1,GO!”,这个布尔值设置为true倒数计时器在我的游戏开始时结束。在布尔值设置为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 state

以下新代码

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);
        }
    }   
}

2 个答案:

答案 0 :(得分:0)

  

我有这个爬行动画,它在一开始就播放   游戏(甚至在计时器结束之前)并永远循环。我的问题是,   如何使该爬行动画也在布尔值开始时启动   设置为&#34; true&#34;?

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

选择附加了PlaneMover脚本的GameObject,将动画和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);
        }
    }
} }

答案 1 :(得分:0)

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

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”时启用我在Inspector中附加的Animator,为了增加安全性,我添加了“else”以使其被禁用。如果有人注意到我改进此脚本的方式,请告诉我。谢谢:))

更短的版本:

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