Unity-仅在动画停止时更改角色 [英] Unity - Change character only when animation stopped

查看:145
本文介绍了Unity-仅在动画停止时更改角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象(相同的角色,但是功能不同),我希望在动画停止和单击触发运行时更改角色.例如,我在Kick_Player中有单击触发的动画,并且当Kick_Player结束动画时,我希望它自动更改为Player_Stopped.姿势互不相同,因为我需要进行这些更改.

i have two objects(the same character, but, in different functions) which i want change the character when animation stop and when it runs triggered by a click. For example, I have the Kick_Player where there is the animation triggered by the click, and when the Kick_Player ends the animation, i want it automatically changes to Player_Stopped. The poses are different each other, 'cause this i need to do these changes.

我尝试使用this.MyAnimator.GetCurrentAnimatorStateInfo(0).IsName("My_Animation")进行操作,但是尝试失败.有办法吗?

I tried something with this.MyAnimator.GetCurrentAnimatorStateInfo(0).IsName("My_Animation") but, i got unsuccessful tries. Is there a way to do that ?

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

}

我编写了此代码进行测试,但无法正常工作

I made this code to test, but it doesn't work

推荐答案

使用IsName函数要求您在实际动画状态之前为动画状态的基本层名称添加前缀.

Using the IsName function requires that you prefix the base layer name of the animation state before the actual animation state.

默认基本名称通常为基本层"

if(!this.Anim.GetCurrentAnimatorStateInfo(0).IsName("Base Layer.Kick_Ball") 

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

Note that you have to do that outside your if(input.GetMouseButtonDown(0)){ otherwise that will never get chance to be checked.

我已经看到IsName不适用于某些人的报告,因此,如果您这样做但仍然有问题,请考虑以其他方式进行.

I've seen reports of IsName not working for some people so if you do that but still have issues, consider doing it another way.

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

这篇关于Unity-仅在动画停止时更改角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆