在Unity中的某些动画期间,动画可实现平滑的过渡和碰撞

时间:2019-07-19 04:51:56

标签: c# unity3d animation collision

我希望我的动画跳起来,并且确实抬高了地面,我希望我的攻击动画能够启动,但是没有。与敌人碰撞不会导致过渡,因为整个身体都找不到碰撞器或触发器。我的意思是,与没有动画的简单精灵相比,我无法找到哪个触发器受动画影响,而仅使用boxcollider2D来感知场景中其他对象的触发器。

这是设置动画从一种状态到另一种状态的方式。等于GetCurrentAnimatorStateInfo(0)的AnimatorStateInfo变量;使用Animator.StringToHash(“ ...”)将其与存储为整数的字符串进行比较。但这对我没用。我的gameObject只有两个状态:空闲或防御。现在它只有一个,那就是空闲。哪个字符串进入StringToHash函数?是动画本身的名称,还是某个参数的名称?我将向您显示代码并留下注释。请咨询...

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

Defend Animation的响应速度非常慢,而且最好直到我找到并实现正确的方法后才开始工作。在将这些方法实现到脚本中之前,我还尝试实现与非动画精灵相同的触发概念,但这没有用。我对OnTriggerStay2D或OnTriggerExit2D或OnTriggerEnter2D函数是否适用于动画或是否存在完全不同的方法有疑问。感谢您的任何答复。请说明一下。

1 个答案:

答案 0 :(得分:0)

好的,我知道了!在C-Sharp(C#)中,所有操作都由字符串完成,整个nameHash / fullPathHash方法必须用于其他用途。与其使用AnimatorStateInfo访问动画状态并将该变量与nameHash进行比较,我们不使用AnimatorClipInfo进行操作并标识片段名称。剪辑名称指定了当前正在播放的动画,我们已经准备好过渡到下一个动画。这是起作用的代码:

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