首页 > 解决方案 > Unity 中某些动画期间平滑过渡和碰撞的动画

问题描述

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

这是我如何设置动画从一种状态到另一种状态的过渡。一个等于 GetCurrentAnimatorStateInfo(0) 的 AnimatorStateInfo 变量;使用 Animator.StringToHash("..."); 与存储为整数的字符串进行比较;但它从来没有对我有用。我的游戏对象只有两种状态:空闲或防御。现在它只有一个,那就是空闲。哪个字符串进入 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 函数是否适用于动画或者是否有完全不同的方法有疑问。感谢您的任何回复。请阐明一些观点。

标签: c#unity3danimationcollision

解决方案


好吧,我想通了!在 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);
    }
}

推荐阅读