首页 文章

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

提问于
浏览
1

我对使用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

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

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

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

相关问题