简体   繁体   English

Unity中某些动画期间的平滑过渡和碰撞的动画

[英]Animations for smooth transitions and Collisions during certain animations in Unity

I expected my animation to jump and it did lift off the ground and I expected my attack animation to initiate however it did not. 我希望我的动画跳起来,并且确实抬高了地面,我希望我的攻击动画能够启动,但是却没有。 Colliding with enemies causes no transition because the whole body cannot find a collider or trigger. 与敌人碰撞不会导致过渡,因为整个身体都找不到碰撞器或触发器。 I mean, I cannot find which trigger is affected by animations compared to simple sprites that do not animate and just use boxcollider2D's to sense triggers from other objects in the scene. 我的意思是,与不设置动画的简单精灵相比,我无法找到哪个触发器受动画影响,而仅使用boxcollider2D来感知场景中其他对象的触发器。

Here is how I set up my animation's transition from one state to another. 这是设置动画从一种状态到另一种状态的过渡方式。 An AnimatorStateInfo variable equal to GetCurrentAnimatorStateInfo(0); 等于GetCurrentAnimatorStateInfo(0)的AnimatorStateInfo变量; is compared to a string stored as an integer using Animator.StringToHash("..."); 使用Animator.StringToHash(“ ...”)将其与存储为整数的字符串进行比较。 but it never worked for me. 但这对我没用。 My gameObject has only two states: Idle or Defend. 我的gameObject只有两个状态:空闲或防御。 Right now it only has one and that's Idle. 现在它只有一个,那就是空闲。 Which string goes into the StringToHash function? 哪个字符串进入StringToHash函数? is it the name of the Animation itself, or is it the name of a certain parameter? 是动画本身的名称,还是某个参数的名称? I'll show you code and leave notes. 我将向您显示代码并留下注释。 please inquire ... 请咨询...

int idleHash = Animator.StringToHash("...");
//stored as an integer and I Don't Know what to put into the 
//parenthesis.
//Then a function is called to go into the Defend Animation.

void defendStart()    {
    AnimatorStateInfo Variable1 = GetComponent<Animator>.  
    ().GetCurrentAnimatorStateInfo(0);
//Keep in mind the next IF statement is compared with an integer   
//stored value given to us by the Animator and this is where I'm  
//not sure if the string for the nameHash is related to a certain  
//parameter or if it's the title of the Animation itself
    if (stateInfo.nameHash == idleHash)    {
        GetComponent<Animator>().SetBool("Defend", true);
                                            }
                       }

The Defend Animation was very slow to respond and better off not working at all until I can find and implement the proper method. Defend Animation的响应速度很慢,而且最好直到我找到并实施适当的方法后才开始工作。 Before implementing these methods into the script, I also tried to implement the same trigger concept as is to non-animated sprites but this did not work. 在将这些方法实现到脚本中之前,我还尝试实现与非动画精灵相同的触发概念,但这没有用。 I had questions about whether the OnTriggerStay2D or OnTriggerExit2D or OnTriggerEnter2D functions worked with animations or if there is a completely different method. 我对OnTriggerStay2D或OnTriggerExit2D或OnTriggerEnter2D函数是否适用于动画或是否存在完全不同的方法有疑问。 Thank you for any responses. 感谢您的任何答复。 Please shed some light. 请说明一下。

Alright, I figured it out! 好吧,我知道了! In C-Sharp (C#) it is all done with strings and the entire nameHash/fullPathHash method must be for something else. 在C-Sharp(C#)中,所有操作都由字符串完成,整个nameHash / fullPathHash方法必须用于其他用途。 Instead of accessing our Animation state with AnimatorStateInfo and comparing that variable to a nameHash, we do it with AnimatorClipInfo and identify the clip name. 与其使用AnimatorStateInfo访问动画状态并将该变量与nameHash进行比较,我们不使用AnimatorClipInfo进行操作并标识片段名称。 The clip name specifies what animation is currently playing and that we are ready to transition into the next animation. 剪辑名称指定了当前正在播放的动画,我们已经准备好过渡到下一个动画。 Here is the code that worked: 这是起作用的代码:

AnimatorClipInfo[0] m_AnimClipInfo;
string m_ClipName;

//Then a function is called to go into the defend   
//animation.

void defendStart()
{
    m_AnimClipInfo = this.GetComponent<Animator>.  
    ().GetCurrentAnimatorClipInfo(0);
    m_ClipName = m_AnimClipInfo[0].clip.name;
    Debug.Log(m_ClipName);

//without this next IF statement, animations would lag,  
//skip, or otherwise not begin immediately on call.

//Where it says, '(name of your animation)' you put the 
//Debug.Log function's output.

    if (m_ClipName == "(name of your animation)")
    {
    GetComponent<Animator>().SetBool("Defend", true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM