【问题标题】:How to check if a certain animation state from an animator is running?如何检查动画师的某个动画状态是否正在运行?
【发布时间】:2018-10-30 23:13:06
【问题描述】:

我创建了一个名为“m4a4animator”的动画师。在它里面,主函数叫做“idle”(无),还有其他2个状态:“shoot”(mouse0)和“reload”(R)。这 2 个动画状态转换为“空闲”。现在,一切正常......但我唯一的问题是:如果我正在重新加载并按下鼠标0(射击),动画运行状态立即变为射击......但我想阻止它.

现在的问题是:如何在动画运行时停止某些动画更改?

Here is my animator

这是我的脚本:

using UnityEngine;
using System.Collections;

public class m4a4 : MonoBehaviour {

    public Animator m4a4animator;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown (KeyCode.R)) {

            m4a4animator.Play("reload");

        }

        if (Input.GetMouseButton(0)) {

            m4a4animator.Play("shoot");

        }
    }
}

【问题讨论】:

  • 当然可以。
  • 好问题。您最好将图片直接添加到您的问题中,因为链接地址可以更改。
  • 如果您使用的是 Mecanim,那么我认为您不必添加检查动画是否正在运行。只需在“重新加载 --> 空闲”的转换上勾选“有退出时间”。这应该可以解决问题。
  • 另外我建议您使用触发器而不是animator.play。这些链接应该会有所帮助:- 1)docs.unity3d.com/Manual/AnimationParameters.html 2) youtube.com/watch?v=N73EWquTGSY

标签: c# animation unity3d animator


【解决方案1】:

对于旧版动画系统,Animation.IsPlaying("TheAnimatonClipName) 用于检查动画剪辑是否正在播放。


对于新的 Mechanim Animator 系统,您必须检查 anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName)anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f) 是否为真。如果是,则当前正在播放动画名称。

这可以像上面的Animation.IsPlaying函数一样简化。

bool isPlaying(Animator anim, string stateName)
{
    if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&
            anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f)
        return true;
    else
        return false;
}

现在,一切正常......但我唯一的问题是:如果 我正在重新加载,然后按 mouse0(射击), 动画运行状态立即更改为拍摄...但我想 阻止它。

按下拍摄按钮时,检查“重新加载”动画是否正在播放。如果是,请不要开枪。

public Animator m4a4animator;
int animLayer = 0;

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.R))
    {
        m4a4animator.Play("reload");
    }

    //Make sure we're not reloading before playing "shoot" animation
    if (Input.GetMouseButton(0) && !isPlaying(m4a4animator, "reload"))
    {
        m4a4animator.Play("shoot");
    }
}

bool isPlaying(Animator anim, string stateName)
{
    if (anim.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) &&
            anim.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f)
        return true;
    else
        return false;
}

如果您需要在播放“射击”动画之前等待“重新加载”动画完成播放,请使用协程。 This 帖子描述了如何做到这一点。

【讨论】:

    【解决方案2】:

    还有其他话题:https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html

    if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName"))
    {
        //your code here
    }
    

    这会告诉您您是否处于某种状态。

    Animator.GetCurrentAnimatorStateInfo(0).normalizedTime
    

    这会给你动画的标准化时间:https://docs.unity3d.com/ScriptReference/AnimationState-normalizedTime.html

    尝试使用这些功能,希望能解决你的问题

    【讨论】:

      猜你喜欢
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2018-01-09
      • 2016-04-16
      相关资源
      最近更新 更多