如何检查动画师的某个动画状态是否为运行?
How to check if a certain animation state from an animator is running?
我创建了一个名为 "m4a4animator" 的动画师。在它内部,主要函数被称为 "idle"(无),其他 2 个状态:"shoot"(mouse0)和 "reload"(R)。这 2 个动画状态转换为 "idle"。现在,一切正常......但我唯一的问题是:如果我正在重新加载并按下 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");
}
}
}
还有其他相关主题: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
尝试把玩那些功能,希望能解决你的问题
对于遗留动画系统,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.
按下拍摄按钮时,检查是否正在播放 "reload" 动画。如果是,请不要开枪。
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;
}
如果您需要等待 "reload" 动画播放完毕后再播放 "shoot" 动画,那么请使用协程。 post 描述了如何操作。
我创建了一个名为 "m4a4animator" 的动画师。在它内部,主要函数被称为 "idle"(无),其他 2 个状态:"shoot"(mouse0)和 "reload"(R)。这 2 个动画状态转换为 "idle"。现在,一切正常......但我唯一的问题是:如果我正在重新加载并按下 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");
}
}
}
还有其他相关主题: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
尝试把玩那些功能,希望能解决你的问题
对于遗留动画系统,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.
按下拍摄按钮时,检查是否正在播放 "reload" 动画。如果是,请不要开枪。
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;
}
如果您需要等待 "reload" 动画播放完毕后再播放 "shoot" 动画,那么请使用协程。