前两天开发游戏时遇到一个十分令人心态爆炸的问题,我在做一个角色动画控制的时候,想用trigger来控制角色的攻击,但是测试的时候发现攻击动画会触发两次,后来找了半天终于看到了b站傅老師MrFu的黑魂复刻的视频中的解决方案。
方法步骤
1.假设我们有以下状态控制机
Idle状态到Attack状态需触发命名为attack的Trigger,从Attack回到Idle则不用任何条件,如果控制Trigger触发时,连续播放了两次Attack的动画就可以继续使用接下来的步骤。
2.选中Attack
为其挂载一个新代码命名为FSMCleaSignals,并将以下代码粘贴于代码页中。
public class FSMCleaSignals : StateMachineBehaviour
{
public string[] ckearAtEnter;
public string[] ckearAtExit;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
foreach (var signal in ckearAtEnter)
{
animator.ResetTrigger(signal);
}
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
foreach (var signal in ckearAtExit)
{
animator.ResetTrigger(signal);
}
}
// OnStateMove is called right after Animator.OnAnimatorMove()
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that processes and affects root motion
//}
// OnStateIK is called right after Animator.OnAnimatorIK()
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that sets up animation IK (inverse kinematics)
//}
}
3.回到unity中调整参数(也可以代码里动态赋值)
Element中填的是你要清空的Trigger的名字。
4.也可以把代码挂载在Idle上
如果将代码挂载在Idle上,则将需要清空的Trigger填在CkearAtEnter中。
全文的思路来源于:https://www.bilibili.com/video/av21513489/?p=14
|