如果满足某些条件,请激活Unity动画吗?

时间:2019-09-22 13:07:28

标签: unity3d animation triggers game-engine animator

我正在Unity中开发2D游戏,但遇到了问题。当出于某种原因尝试为我的一个动画创建动画过渡时,Unity会立即或非常快速地停用我的动画(例如在激活它之后不到一秒钟)。但是,当我没有过渡到动画时,动画效果很好。但是我需要能够将游戏对象转换回满足特定条件后动画之前的状态,因此我不能让动画继续运行。有谁知道我该如何解决这个问题?

我尝试通过尝试bool和触发器来切换我激活动画的方式,以查看是否可能与此相关,条件是确定何时应发生过渡但出现相同问题的条件

这是我如何激活动画的粗略概述

public class InteractControl : MonoBehaviour, IPooledObject
{ 
    public static float timeLeft = 10f;

    public void OnObjectSpawn()
    {
        anim = GetComponent<Animator>();
    }

    // Change this method to just be a temporary obstacle
    void Update() 
    {
        timeLeft -= Time.deltaTime;

        if (timeLeft > 0)
        {
            anim.enabled = true;
        }


        if (timeLeft < 0)
        {
            anim.SetTrigger("FollowAd");
        }
    }
}

广告跟随转换:

Ad Follow with transition

广告关注但不进行转换:

enter image description here

转换设置:

enter image description here

如果timeLeft大于0,则动画应被激活,并且当动画0小于anim.SetTrigger("FollowAd"); 时应立即将其停用,并将游戏对象返回到动画开始之前的状态。现在,如果Ad Follow_没有过渡,则动画会在被调用时起作用,但不会停用。如果Ad Follow_具有与其连接的空状态,应转换为空状态

timeLeft < 0
如上图所示,它被调用

的时间少于一秒钟或根本没有,然后直接切换回默认状态。仅应在glTranslatef(-50.f, -50.f, 0.0f)时切换回去。

1 个答案:

答案 0 :(得分:0)

您可以为所需的动画创建public RuntimeAnimatorController animCon。当您的条件得到满足时。您可以执行anim.runtimeanimatorcontroller = animCon;

当条件为假时,执行此anim.runtimeanimatorcontroller = null;。它不是最好的方法,但是比大多数方法都快和更好。