中断状态机_等等,我改变了主意! 状态机转换中断

中断状态机

I recently investigated a tricky bug reported by a user involving the combination of empty states, override layers, and transition interruptions. As I was digging in, I found that our documentation about transition interruptions in the animation system was a bit… minimalist. A long conversation with my team later, we concluded that a blog post was in order.

我最近调查了一个用户报告的棘手错误,该错误涉及空状态,覆盖层和转换中断的组合。 当我深入研究时,我发现我们有关动画系统中过渡中断的文档有点……极简。 经过与我的团队的长时间交谈,我们得出的结论是,博客文章井井有条。

So let’s dive into some intricate details of State Machine Transitions and interruptions!

因此,让我们深入研究一下状态机转换和中断的一些复杂细节!

By default in the animation system, transitions cannot be interrupted: once you start going from one state to the other, there’s no way out. Like a passenger on a transatlantic flight, you’re cozily nestled in your seat until you reach your destination and you can’t change your mind. For most users, this is fine.

在动画系统中,默认情况下,过渡不会中断:一旦从一种状态转到另一种状态,就没有出路了。 就像跨大西洋航班上的乘客一样,您一直被困在座位上,直到到达目的地,而且您无法改变主意。 对于大多数用户来说,这很好。

But if you need more control over transitions, Mecanim can be configured in a variety of ways to meet your needs. If you’re unhappy with your current destination, you can hop in the pilot’s seat and can change plans midway through your flight. This means more responsive animations, but also many opportunities to get lost in the complexity.  

但是,如果您需要对过渡进行更多控制,则可以采用多种方式配置Mecanim以满足您的需求。 如果您对当前的目的地不满意,可以跳到飞行员的座位上,并在飞行途中更改计划。 这意味着动画的响应速度更快,但也有很多机会迷失于复杂性中。

So let’s walk through a few examples to sort that out. We can begin with a fairly simple state machine with four states, labeled A to D, and triggers hooked to every transition on the state machine.

因此,让我们通过一些示例进行梳理。 我们可以从一个非常简单的状态机开始,该状态机具有四个状态,标记为A到D,并且触发器与状态机的每个转换挂钩。

image05

By default, when we trigger the A->B transition, our state machine transitions towards B and nothing can keep it from reaching its destination. But if we go on the A->B transition inspector and change the interruption source from “None” to “Current State”, our journey from A to B can be interrupted by some triggers on state A.

默认情况下,当我们触发A-> B转换时,状态机将向B转换,没有什么可以阻止其到达目的地。 但是,如果我们使用A-> B过渡检查器并将中断源从“无”更改为“当前状态”,则 状态A上 的 某些 触发器 可能会中断我们从A到B的旅程 。

image01

Why only “some”? Because the “Ordered Interruption” checkbox is also checked by default. This means only transitions on state A that have a higher priority than the current one are allowed. Looking at the inspector of state A, we can see that this only applies to the A->C transition.

image02

So if we activate the A->B trigger, then shortly after the A->D trigger, our transition remains uninterrupted. However, if we press the A->C trigger instead, then the transition is immediately interrupted and the state machine starts transitioning towards C. 

为什么只有“一些”? 因为默认情况下也会选中“有序中断”复选框。 这意味着只允许状态A上具有比当前状态更高优先级的转换。 查看状态A的检查器,我们可以看到这仅适用于A-> C转换。
因此,如果我们激活A-> B触发器,然后在A-> D触发器之后不久,我们的过渡就不会中断。 但是,如果我们改为按A-> C触发器,则转换将立即中断,并且状态机将开始向C转换。

Internally, the animation system records the pose at the time of the interruption, and will now blend between that static pose (X) and the new destination animation.

image04
Why a static pose, instead of a possibly smoother blend between the current and new transitions? Simply put: performance. When a game faces a cascade of interruptions, keeping track of several dynamic transitions taking place simultaneously would quickly made the animation system unscalable.

在内部,动画系统会记录中断时的姿势,现在将在该静态姿势(X)和新的目标动画之间进行融合。 为什么要使用静态姿势,而不是在当前过渡和新过渡之间进行更平滑的混合? 简而言之:性能。 当游戏面临一系列中断时,跟踪同时发生的多个动态过渡会很快使动画系统无法缩放。

Now, if we uncheck that “Ordered Interruption” checkbox, then both A->C and A->D can interrupt the transition. However, if they are both triggered on the same frame, A->C will still take precedence because it has a higher priority.

现在,如果取消选中“有序中断”复选框,则A-> C和A-> D都可以中断转换。 但是,如果它们都在同一帧上触发,则A-> C仍将具有优先权,因为它具有更高的优先级。

If we change the interruption source to “Next State”, A->C and A->D can no longer interrupt the transition, regardless of their order. However, if we press the B->D trigger, we will immediately start transitioning from A to D, without completing the transition towards B.
Transition order matters on state B too. The “Ordered Interruption” checkbox is not available anymore (any triggered transition on B can interrupt the transition because they do not have a priority ranking relative to A->B), but the order of the transitions on B will determine which transition wins if both are triggered within the same frame. In this case, if B->D and B->C are triggered in the same frame, B->D will be selected.

如果我们将中断源更改为“下一个状态”,则无论其顺序如何,A-> C和A-> D都无法再中断转换。 但是,如果我们按B-> D触发器,我们将立即开始从A到D的过渡,而无需完成向B的过渡。
转换顺序对状态B也很重要。 “有序中断”复选框不再可用(B上任何触发的转换都可以中断转换,因为相对于A-> B,它们没有优先级),但是B上转换的顺序将确定哪一个转换获胜两者都在同一帧内触发。 在这种情况下,如果在同一帧中触发了B-> D和B-> C,则将选择B-> D。

image03

Finally, for complete control, we can set the Interruption Source to “Current State Then Next State”, or “Next State Then Current State”. In that case, the transitions will be analyzed independently on one state, then the other.
So, let’s assume we have the following configuration.

最后,为了完全控制,我们可以将中断源设置为“当前状态,然后是下一个状态”,或“下一个状态,然后是当前状态”。 在这种情况下,将在一种状态下然后在另一种状态下独立分析转换。
因此,假设我们具有以下配置。

image00

During the A->B transition, a very excited player triggers four transitions within the same frame: A->C, A->D, B->C and B->D. What happens?
First, “Ordered Interruption” is checked, so we can ignore A->D right away: it has lower priority than A->B. The current state gets resolved first, so we do not even have to look at state B to know that transition A->C wins.

在A-> B过渡期间,非常激动的玩家会在同一帧内触发四个过渡:A-> C,A-> D,B-> C和B-> D。 怎么了?
首先,检查“有序中断”,因此我们可以立即忽略A-> D:它的优先级低于A-> B。 当前状态首先得到解决,因此我们甚至不必查看状态B即可知道转换A-> C获胜。

image02
image03

However, with the same configuration, if only B->C and B->D get triggered, transition B->D will take place (it has greater precedence than B->C).  
但是,在相同的配置下,如果仅触发B-> C和B-> D,则将发生过渡B-> D(其优先级高于B-> C)。
Now, this is only for one transition… All other transitions can also be interruptible too, with their own specific rules. So if we make transition A->C interruptible from the next state, we can have transition A->B interrupted by A->C which in turn could be interrupted by C->D.
现在,这仅适用于一个转换...所有其他转换也可以使用其自己的特定规则来中断。 因此,如果使转换A-> C从下一个状态可中断,我们可以使转换A-> B被A-> C中断,而转换A-> B又可以被C-> D中断。
One important thing to keep in mind: regardless of interruptions taking place, the source state remains the same until the transition is complete, and Animator.GetCurrentAnimatorStateInfo() will always return that source state.
要记住的一件事是:无论发生什么中断,在过渡完成之前,源状态都将保持不变,并且Animator.GetCurrentAnimatorStateInfo()将始终返回该源状态。
In short, transition interruptions settings are powerful and offer a lot of flexibility, but they can quickly become very confusing. So use transition interruptions wisely, and when in doubt, test it out in the Editor.
简而言之,过渡中断设置功能强大且提供了很大的灵活性,但它们很快就会变得非常混乱。 因此,请明智地使用过渡中断,如有疑问,请在编辑器中进行测试。

翻译自: https://blogs.unity3d.com/2016/07/13/wait-ive-changed-my-mind-state-machine-transition-interruptions/

中断状态机

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值