gpt4 book ai didi

c# - 如何检查动画师的某个动画状态是否正在运行?

转载 作者:行者123 更新时间:2023-11-30 20:26:06 26 4
gpt4 key购买 nike

我创建了一个名为“m4a4animator”的动画师。在其中,主要功能称为“空闲”(无),以及其他 2 个状态:“射击”(mouse0)和“重新加载”(R)。这 2 个动画状态转换为“空闲”。现在,一切正常......但我唯一的问题是:如果我正在重新加载并按下 mouse0(射击),动画运行状态立即变为射击......但我想阻止它.

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

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

}
}
}

最佳答案

对于遗留动画系统,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;
}

Now, everything is working... but the only problem I have is this: if I am in the middle of reloading and and press mouse0 (shoot), the animation running state immediately changes to shoot... but I want to block that.

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

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帖子描述了如何这样做。

关于c# - 如何检查动画师的某个动画状态是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50446427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com