-
Notifications
You must be signed in to change notification settings - Fork 127
Transition Types
Inspiaaa edited this page May 28, 2024
·
1 revision
UnityHFSM provides multiple different transition types that you can use to significantly reduce the amount of code required to achieve certain behaviours.
As with the state types, the base transition classes use generics to allow you to use different types for the identifiers of states. Thanks to class overloads, boilerplate code is kept to a minimum for the most common use case (using strings for the state names).
The base class of all transition types is the TransitionBase<TStateId>
class. The higher-level transition types inherit from this class and provide additional features, such as the support for conditions.
If you are implementing you own transition class, it is recommended to inherit from TransitionBase<TStateId>
.
- Base class of all transitions.
- Condition-less => the transition should always occur.
new TransitionBase("FromState", "ToState", forceInstantly: false);
- Generic type parameters:
TransitionBase<TStateId>
new TransitionBase<int>(0, 1, forceInstantly: false);
- Generic overloads:
-
TransitionBase
=TransitionBase<string>
-
- Default transition class.
- Lets you define a custom condition that is checked to see whether the state machine should transition or not.
new Transition("From", "To");
new Transition("From", "To", forceInstantly: true);
new Transition("Idle", "Follow", self => DistanceToPlayer < 5);
- Generic type parameters:
Transition<TStateId>
- Generic overloads:
-
Transition
=Transition<string>
-
- Waits a certain delay before transitioning.
- Lets you define a custom condition that is checked after the delay has elapsed.
// Waits for 2 seconds
new TransitionAfter("From", "To", 2);
new TransitionAfter("From", "To", 2, forceInstantly: true);
new TransitionAfter("From", "To", 2, self => DistanceToPlayer < 5);
- Generics and overloads are the same as
Transition
(TransitionAfterDynamic<TStateId>
)
- Same as
TransitionAfter
but lets you dynamically compute the delay value:
new TransitionAfterDynamic("From", "To", self => ComputeDelay());
new TransitionAfterDynamic(
"From",
"To",
delay: self => ComputeDelay(),
condition: self => DistanceToPlayer < 5
);
- It also has the option to only evaluate the dynamic delay when the
from
state enters. This is useful, e.g. when the delay of a transition should be random. E.g.
fsm.AddTransition(new TransitionAfterDynamic(
"From", "To", t => Random.Range(2, 10), onlyEvaluateDelayOnEnter: true
));
- Generics and overloads are the same as
Transition
(TransitionAfter<TStateId>
)
- Transition types that trigger when a specific key is pressed / released / ...
new TransitionOnKey.Down("A", "B", KeyCode.Space); // Like Input.GetKey(...)
new TransitionOnKey.Pressed("A", "B", KeyCode.Space); // Like Input.GetKeyDown(...)
new TransitionOnKey.Released("A", "B", KeyCode.Space); // Like Input.GetKeyUp(...)
new TransitionOnKey.Up("A", "B", KeyCode.Space); // Like !Input.GetKey(...)
- Generics and overloads are the same as
Transition
(TransitionOnKey<TStateId>
).
- Transition types that trigger when a specific mouse button is pressed / released / ...
new TransitionOnMouse.Down("A", "B", 0); // Like Input.GetMouseButton(...)
new TransitionOnMouse.Pressed("A", "B", 0); // Like Input.GetMouseButtonDown(...)
new TransitionOnMouse.Released("A", "B", 0); // Like Input.GetMouseButtonUp(...)
new TransitionOnMouse.Up("A", "B", 0); // Like !Input.GetMouseButton(...)
- Generics and overloads are the same as
Transition
(TransitionOnMouse<TStateId>
).