【Godot】move_and_slide vs move_toward: Implementing Knockback

Created: 2025-06-20Last updated: 2025-12-06

How to combine move_and_slide and move_toward in CharacterBody2D movement processing. Knockback effect implementation examples

Overview

When trying to move characters in Godot, you'll encounter two similarly-named functions: move_and_slide() and move_toward(). Both are for moving objects, but their underlying mechanisms and optimal uses are completely different.

Understanding the difference between these two is crucial for implementing smooth physics-based movement and game logic like knockback as intended.

Godot Knockback Processing

move_and_slide(): A Resident of the Physics World

move_and_slide() is a highly functional movement method exclusive to physics characters like CharacterBody2D and CharacterBody3D.

Key Features:

  • Physics-based: Works closely with Godot's physics engine, automatically handling collisions
  • Sliding Motion: When hitting a wall, objects don't just stop - they "slide" along the wall. This dramatically improves wall-adjacent controls
  • Handles Slopes and Moving Platforms: Complex internal processing handles smooth slope navigation and correct behavior on moving platforms
  • Return Value: Executing move_and_slide() automatically updates the character's velocity property based on collisions

Primary Uses:

  • Player character and NPC movement that should correctly collide with walls and floors
  • Almost any case requiring natural physics-based movement
# Basic pattern: call every frame in _physics_process
func _physics_process(delta):
    # Calculate and set velocity
    velocity.x = direction * SPEED
    velocity.y += gravity * delta

    # Let physics engine handle movement and collisions
    move_and_slide()

move_toward(): Simple Mathematical Movement

move_toward() is a general-purpose mathematical function that "changes a vector or value by a specified amount toward a target value." It has nothing to do with the physics engine.

Key Features:

  • Ignores Physics: This function doesn't detect collisions. It simply moves objects to specified coordinates mathematically
  • Constant Speed: Regardless of current position or target value, always changes values at the specified constant step
  • Versatile: Can be used not just for Vector2, but for float, Color, and various other data types (e.g., gradually changing Color)

Primary Uses:

  • Moving objects that don't consider physical collisions (e.g., UI elements, background objects)
  • Forcibly moving characters while temporarily ignoring physics in specific states (e.g., during knockback)
  • Changing values other than movement at constant speed (e.g., HP bar smoothly decreasing)

Practical Example: Implementing Knockback

The distinction between these two functions really shines when implementing "knockback" - that action where a player hit by an enemy gets pushed back.

Use move_and_slide() for normal movement, but switch to move_toward() (or direct velocity manipulation) during knockback to achieve the intended behavior.

var is_knockback = false
var knockback_vector = Vector2.ZERO

func _physics_process(delta):
    if is_knockback:
        # During knockback, directly manipulate velocity, use move_and_slide only for collision
        velocity = knockback_vector
        move_and_slide()
        # Gradually decelerate
        knockback_vector = knockback_vector.move_toward(Vector2.ZERO, 200 * delta)
        if knockback_vector == Vector2.ZERO:
            is_knockback = false
    else:
        # Normal movement processing
        # ... (velocity calculation based on Input)
        move_and_slide()

# Function called externally when taking damage
func apply_knockback(direction):
    is_knockback = true
    knockback_vector = direction * 400 # Direction and force of knockback

In this example, state is managed with an is_knockback flag. During knockback, player input (normal movement) is ignored, and knockback_vector gradually approaches zero using move_toward, creating natural deceleration.

Summary

move_and_slide() and move_toward() are functions with completely different roles despite looking similar.

  • move_and_slide(): The star of physics character normal movement. Leave collision handling to it
  • move_toward(): For mathematical movement ignoring physics. UI and special movement effects

Understanding this difference and using them appropriately will make your game characters move more vividly and as intended.