【问题标题】:Make Animation Play After Boolean is Set to True?布尔值设置为真后播放动画?
【发布时间】:2016-10-13 20:59:53
【问题描述】:

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

public bool countDownDone = false;

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

【问题讨论】:

    标签: c# animation unity3d boolean unity5


    【解决方案1】:

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

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

    选择您的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);
            }
        }
    } }
    

    【讨论】:

    • @StephenGeorge 你是对的。这是旧的,但 Unity 不会很快删除它,因为许多项目仍在使用它。我只是在 Unity 的网站上查看,它不会很快被删除。建议您使用新的 Animator API。
    • @StephenGeorge 我的回答几乎是一样的。简单地在我的答案中删除动画 API 并将其替换为 Animator。要播放它,请使用Animator.Play("stateName")stateName 是 Mechanim 中创建的动画状态的名称。
    • @StephenGeorge 将 Edit 放在问题的末尾,然后添加新代码并告诉我它有什么问题。我没有与机械师合作过,但会看看我能做什么。
    • @StephenGeorge 在你的问题中而不是在我的答案中。
    • @StephenGeorge 没关系。我拒绝了编辑。现在发生了什么?它有效吗?有什么错误吗?
    【解决方案2】:

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

    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; 
    

    【讨论】:

    • 因为countDownDone 是布尔型,你可以只做 if(GMS.countDownDone)。此外,由于您只在 if 和 else 语句中运行一个命令,您可以通过删除 {} 来缩短它。所以现在你有if (GMS.countDownDone) animator.enabled = true; else animator.enabled = false;
    • 感谢您的改进 :) @Programmer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多