C# Unity-仅在动画停止时更改角色

C# Unity-仅在动画停止时更改角色,c#,unity3d,C#,Unity3d,我有两个对象(同一个角色,但功能不同),我希望在动画停止和单击触发运行时更改角色。例如,我有Kick\u播放器,其中有由单击触发的动画,当Kick\u播放器结束动画时,我希望它自动更改为Player\u Stopped。姿势各不相同,因为我需要做这些改变 我尝试使用this.MyAnimator.GetCurrentAnimatorStateInfo(0).IsName(“My_动画”)执行某些操作,但尝试失败。有办法吗 public class TapController : MonoBeha

我有两个对象(同一个角色,但功能不同),我希望在动画停止和单击触发运行时更改角色。例如,我有
Kick\u播放器
,其中有由单击触发的动画,当
Kick\u播放器
结束动画时,我希望它自动更改为
Player\u Stopped
。姿势各不相同,因为我需要做这些改变

我尝试使用
this.MyAnimator.GetCurrentAnimatorStateInfo(0).IsName(“My_动画”)
执行某些操作,但尝试失败。有办法吗

public class TapController : MonoBehaviour {

    Animator Anim;

    public GameObject CharacterToController; //Kick_Player
    public GameObject CharacterToBeStopped; //Player_Stopped

     void Start(){
      Anim = CharacterToController.GetComponent<Animator>();
      CharacterToBeStopped.SetActive(false);
     }

     void Update(){
      if(input.GetMouseButtonDown(0)){
       if(!CharacterToController.activeSelf){
        CharacterToController.SetActive(true);
       }

       Anim.Play("Kick_Ball");


     if(!this.Anim.GetCurrentAnimatorStateInfo(0).IsName("Kick_Ball") {
        CharacterToController.SetActive(false);
        CharacterToBeStopped.SetActive(true);
      }
     }

}
公共类TapController:MonoBehavior{
动画师;
公共游戏对象CharacterToController;//Kick\u玩家
公共游戏对象CharacterToBeStopped;//玩家\u已停止
void Start(){
Anim=CharacterToController.GetComponent();
CharacterToBeStopped.SetActive(假);
}
无效更新(){
if(input.GetMouseButtonDown(0)){
if(!CharacterToController.activeSelf){
CharacterToController.SetActive(真);
}
动漫游戏(“踢足球”);
如果(!this.Anim.GetCurrentAnimatorStateInfo(0).IsName(“踢球”){
CharacterToController.SetActive(错误);
CharacterToBeStopped.SetActive(真);
}
}
}

我编写这段代码是为了进行测试,但它不起作用。使用
IsName
函数需要在实际动画状态之前为动画状态的基本层名称加前缀

默认的基础名称通常为“基础层”

请注意,您必须在
if(input.GetMouseButtonDown(0)){
之外执行此操作,否则将永远没有机会进行检查


<>我看到过一些关于“代码> ISNEX <代码>的报告,有些人不适用,所以如果你这样做,但仍然有问题,考虑另一种方式。< /P>
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        if (!CharacterToController.activeSelf)
        {
            CharacterToController.SetActive(true);
        }

        Anim.Play("Kick_Ball");

        StartCoroutine(PlayAndWaitForAnim(Anim, "Kick_Ball"));
    }
}


const string animBaseLayer = "Base Layer";
int animHash = Animator.StringToHash(animBaseLayer + ".Kick_Ball");

public IEnumerator PlayAndWaitForAnim(Animator targetAnim, string stateName)
{
    targetAnim.Play(stateName);


    //Wait until we enter the current state
    while (targetAnim.GetCurrentAnimatorStateInfo(0).fullPathHash != animHash)
    {
        yield return null;
    }

    float counter = 0;
    float waitTime = targetAnim.GetCurrentAnimatorStateInfo(0).length;

    //Now, Wait until the current state is done playing
    while (counter < (waitTime))
    {
        counter += Time.deltaTime;
        yield return null;
    }

    //Done playing. Do something below!
    Debug.Log("Done Playing");

    CharacterToController.SetActive(false);
    CharacterToBeStopped.SetActive(true);
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
if(!CharacterToController.activeSelf)
{
CharacterToController.SetActive(真);
}
动漫游戏(“踢足球”);
开始常规(play和waitforanim(动画,Kick_Ball));
}
}
常量字符串animBaseLayer=“基本层”;
int animHash=Animator.StringToHash(animBaseLayer+“.Kick_Ball”);
public IEnumerator PlayAndWaitForAnim(动画师targetAnim,字符串stateName)
{
targetAnim.Play(stateName);
//等到我们进入当前状态
while(targetAnim.GetCurrentAnimatorStateInfo(0.fullPathHash!=animHash)
{
收益返回空;
}
浮点计数器=0;
float waitTime=targetAnim.GetCurrentAnimatorStateInfo(0).length;
//现在,等待当前状态完成播放
while(计数器<(等待时间))
{
计数器+=时间增量时间;
收益返回空;
}
//完成游戏。做下面的事情!
Log(“完成播放”);
CharacterToController.SetActive(错误);
CharacterToBeStopped.SetActive(真);
}

My_Animation的值是多少?你能展示你是如何播放动画的吗?你可以使用动画事件。你说的值是什么意思?我运行了一个实例,每次更新触发后,它都会运行动画。我会用我的代码更新问题。你说你尝试了
这个。MyAnimator.GetCurrentAnimatorStateInfo(0).IsName(“My_Animation”)
?我在问你My_Animation的值。我指的是你在那里使用的实际字符串。这很重要。我需要知道你在比较什么值。此外,你应该显示播放或触发(代码)动画的方式animation@Programmer我更新了我的问题,我会在这里尝试,稍后我会带来答案
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        if (!CharacterToController.activeSelf)
        {
            CharacterToController.SetActive(true);
        }

        Anim.Play("Kick_Ball");

        StartCoroutine(PlayAndWaitForAnim(Anim, "Kick_Ball"));
    }
}


const string animBaseLayer = "Base Layer";
int animHash = Animator.StringToHash(animBaseLayer + ".Kick_Ball");

public IEnumerator PlayAndWaitForAnim(Animator targetAnim, string stateName)
{
    targetAnim.Play(stateName);


    //Wait until we enter the current state
    while (targetAnim.GetCurrentAnimatorStateInfo(0).fullPathHash != animHash)
    {
        yield return null;
    }

    float counter = 0;
    float waitTime = targetAnim.GetCurrentAnimatorStateInfo(0).length;

    //Now, Wait until the current state is done playing
    while (counter < (waitTime))
    {
        counter += Time.deltaTime;
        yield return null;
    }

    //Done playing. Do something below!
    Debug.Log("Done Playing");

    CharacterToController.SetActive(false);
    CharacterToBeStopped.SetActive(true);
}