Building a platformer AI - Part 5: Character behaviors

An animated GIF of a recording showing various character-movement behaviors in Surfacer.
Various character-movement behaviors.
This is part 5 of 5 in a series describing how to implement 2D-platformer AIpathfinding, and character movement. You can see a working example of these techniques in the Surfacer framework for Godot.


Part 5: Character behaviors

Now that we've learned how to create a platform graph and navigate through it, we can create some powerful high-level character behaviors!

Some general-purpose behavior types

Here is a list of general-purpose behaviors that can be re-used across a wide range of use-cases.

Wander

[source code]

An animated GIF of a recording showing Surfacer wander behavior.
Wander behavior.

A wander behavior periodically moves the character to a random destination within a given range.

  • The range is usually defined as a max distance from a given start position.
  • Each individual navigation could also be limited to a given min and max distance.

Move back-and-forth

[source code]

An animated GIF of a recording showing Surfacer move-back-and-forth behavior.
Move-back-and-forth behavior.

A move-back-and-forth behavior moves the character back-and-forth between two end points.

  • This could be configured as a horizontal radius from a given start position.
  • Or this could be configured to just move between the ends of a given start surface.
  • This could be configured to allow each navigation to use some random offset from the furthest end points.

Climb adjacent surfaces

[source code]

An animated GIF of a recording showing Surfacer climb-adjacent-surfaces behavior.
Climb-adjacent-surfaces behavior.

A climb-adjacent-surfaces behavior moves the character in a path along adjacent surfaces.

For example, the character might walk to the end of a floor surface, then grab an adjacent upward (concave) wall surface, then climb that wall to the ceiling, and so on.

Run away

[source code]

An animated GIF of a recording showing Surfacer run-away behavior.
Run-away behavior.

A run-away behavior moves the character away from a given run-away target.

  • The navigation destination is calculated according to a given run-away-distance and the current direction from the run-away target.
  • This could be either one-shot or ongoing.
    • That is, it could end the behavior as soon as the first path navigation completes.
    • Or it could wait until the target gets close enough, and then re-navigate away to another destination.

Collide

[source code]

An animated GIF of a recording showing Surfacer collide behavior.
Collide behavior.

A collide behavior moves the character toward a given collision target.

Follow

[source code]

An animated GIF of a recording showing Surfacer follow behavior.
Follow behavior.

A follow behavior moves the character toward a given follow target.

  • This usually doesn't try to get closer to the target than a given min distance.
  • This usually doesn't start navigating until the target is further than a given max distance.
  • Optionally, the behavior could be configured to cancel itself when the target is too far away.
  • Usually, a follow behavior should be ongoing, whereas a collide behavior should stop when a collision occurs.

Choreography

[source code]

A choreography behavior moves the character to a given destination or through a given collection of destinations.

Return

[source code]

An animated GIF of a recording showing Surfacer return behavior.
Return behavior (after a follow-target moved too far away).

A return behavior moves the character back to a given origin position.

  • This can be useful for "resetting" a character—for example, after they are done being chased, or have chased something else far enough.

A behavior's "move target"

  • Many behaviors—such as run-away, collide, and follow—are based off of some target, which may be either static or mobile.
  • If the target is mobile:
    • The current navigation destination could be based off of the target's current trajectory/destination, rather than the target's current position.
    • The current navigation destination could be re-computed whenever the target moves to a new surface.
  • These last two bullets can make the AI seem a lot more "intelligent"!

Some general behavior parameters

[source code]

Bounciness

An animated GIF of a recording showing Surfacer move-back-and-forth behavior, with bouncy movement.
Move-back-and forth behavior, with bouncy movement.
  • A character or behavior can be configured to be "bouncy".
  • In this case, whenever a new navigation would be triggered, we can replace any walking edges in the path with consecutive jump-to-the-same-surface edges.
  • Similarly, it can also be useful to configure a behavior to just start or end each navigation with a jump—for example, this can make a collide behavior appear to pounce at the end.

Don't move too far away

  • A behavior can be configured to not move too far from a given start position.
  • This can be useful for ensuring the character doesn't stray too far from their expected region.
  • For example, a guard-dog character might use a collide behavior to chase a nearby player, but stop if the player moves too far away.

Pause between navigations

  • A behavior can be configured to pause between navigations.
  • This pause duration can be randomized within a range.
  • This can help to make movement seem less robotic—especially if there are multiple characters all moving with the same behavior.

Return afterward

  • A behavior can be configured to return back to the start position after it has finished.
  • For example, a hunting dragon might use a collide behavior to attack a player, and then return back to their lair afterward.
  • This return behavior can be automatically instantiated depending on how other behaviors are configured for a given player.

Transitioning between behaviors

  • Custom logic can trigger a new behavior at any time—at which point, the previous active behavior is cancelled.
  • However, many behaviors have a natural end condition, and can automatically trigger the transition to the next behavior.
  • A few different factors can be used to decide which behavior is next in these cases.
    • If a behavior is configured as returning to the start position, then the return behavior will be next.
    • If a behavior has been configured as the default for the character, then it will be next.
      • This is common for behaviors like wander, move-back-and-forth, and climb-adjacent-surfaces.
    • Otherwise, a default rest behavior can be next.

Thanks for reading!

Wow, you made it to the end!

I hope some of this series has been useful to you. If you have any questions, please let me know in the comments!

And if you're interested the Godot game engine and getting platformer AI to work in your game, check out my Surfacer framework.

The Surfacer icon: A path to a destination on a surface, with walk, climb, and jump edges.


Comments