Why would you use Unity’s animation system?

Why not build a quick and dirty implementation instead?

Then how about using a third-party asset?


Okay, how do I start?


How do you use StateMachineBehaviours?


This is the angry pufferfish. He’ll rotate around slowly until he spots the player, and then he’ll propel himself rapidly in the player’s direction.

class RotateFacingDirection : LogicalStateMachineBehaviour {
[SerializeField] private float _rotationSpeed = 10.0f;

...
}

class AimInFacingDirection : LogicalStateMachineBehaviour {
[SerializeField] private LayerMask _targetLayerMask;

void OnStateUpdate() {
this.Animator.SetBool("FacingTarget", this.FacingTarget());
}

...
}

In the PreparingToShoot state, I re-use a state behavior named TriggerContinueAfterDelay with a parameter for how long the delay is.

class TriggerContinueAfterDelay : LogicalStateMachineBehaviour {
[SerializeField] private float _delay = 1.0f;

void OnStateEntered() {
this.DoAfterDelay(this._delay, () => {
this.Animator.SetTrigger("Continue");
}
}
}

And in the Shooting state, I use a MoveInFacingDirection state behavior to move the pufferfish towards the player.

class MoveInFacingDirection : LogicalStateMachineBehaviour {
[SerializeField] private float _speed = 5.0f;

void OnStateUpdate() {
this.MoveInFacingDirection();
}

...
}

Awesome, anything else I should know?