See all glossary terms

Steering behaviors

Steering behaviors are a suite of algorithms used to make playable characters and AIs move smoothly in various ways: following a target, slowing down when arriving at a location, following a path, following each other in a queue, etc.
The most interesting aspect of those behaviors is that they mostly rely on vector arithmetic. So, they are lightweight and generally require little code complexity. They also work the same or nearly the same in 2D and 3D, making them well worth learning.
The simplest and most basic of those behaviors is the follow steering behavior.
It works like this:
  1. We pick a target position we want to reach.
  2. We calculate a desired velocity. It is the velocity we would have if moving straight in the direction of the target at the maximum allowed speed.
  3. We calculate the difference between the current and desired velocity. We call that the steering vector.
  4. We add a fraction of that difference to the current velocity.
The code looks like this:
var max_speed := 600.0
var steering_factor := 10.0

## Reference to the node to follow.
var target: Node2D = null


func _physics_process(delta: float) -> void:
    var direction := Vector2.ZERO
    if target != null:
        direction = global_position.direction_to(target_position)

	var desired_velocity := max_speed * direction
	var steering_vector := desired_velocity - velocity
	velocity += steering_vector * steering_factor * delta
	position += velocity * delta
You can also skip the first step and use this equation to make a playable character accelerate and decelerate smoothly. Here's an example:
var max_speed := 600.0
var steering_factor := 10.0

func _process(delta: float) -> void:
	var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")

	var desired_velocity := max_speed * direction
	var steering_vector := desired_velocity - velocity
	velocity += steering_vector * steering_factor * delta
	position += velocity * delta
This basic steering algorithm is a form of linear interpolation.
You can use steering behaviors in combination with pathfinding to make entities follow a path smoothly and avoid each other while moving along the path. This technique is often used in real-time tactical strategy games (RTS), among other genres.
The most common steering behaviors are:
  • Seek: The entity moves towards a target location.
  • Flee: The character moves away from a target location. It's the opposite of seek.
  • Arrive: This extension of seek slows the character down as it approaches the target to stop more smoothly.
  • Wander: The character moves randomly within a certain area.
  • Pursuit: The character chases another moving character, attempting to predict where they are headed to intercept them. This can be used for homing missiles and mobs.
  • Evade: This is the opposite of pursuit. A character tries to escape from another character, predicting the target's movement.
  • Obstacle avoidance: Characters navigate around obstacles in their environment without relying on a physics engine.
  • Follow path: Characters follow a predefined path, moving through a series of points along the path. The smooth motion this behavior produces pairs well with pathfinding algorithms like Astar or navigation.
  • Alignment: A character gradually adjusts its movement to match the direction and speed of characters around it.
  • Flocking: This simulates the movement of groups of characters like birds or fish. It combines seek, flee, and alignment behaviors to create nice flocking patterns.