How to get collisions after move_and_slide() in Godot 4
By Eduardo Orellana · Updated
After move_and_slide(), loop over get_slide_collision_count() and read each KinematicCollision2D with get_slide_collision(i): its get_collider() is what the body hit. Godot 3's get_slide_count() was renamed and fails to parse with Function "get_slide_count()" not found in base self. Did you mean to use "get_slide_collision_count()"?
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
move_and_slide() can collide several times in one call as it slides along surfaces. Each contact is kept until the next call, and get_slide_collision_count() says how many there were.
get_last_slide_collision() returns the last one directly, and is_on_floor(), is_on_wall() and is_on_ceiling() answer the common questions without reading collisions at all.
Reproduce it
A Godot 3 collision loop, in a Godot 4 project.
extends CharacterBody2D
func _physics_process(_delta):
move_and_slide()
for i in get_slide_count():
print(get_slide_collision(i).get_collider())
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Parse Error: Function "get_slide_count()" not found in base self. Did you mean to use "get_slide_collision_count()"?
at: GDScript::reload (res://answers/how-to-get-collisions-after-move-and-slide-in-godot-4/repro.gd:5)
ERROR: Failed to load script "res://answers/how-to-get-collisions-after-move-and-slide-in-godot-4/repro.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)Fix it
A body falling onto a StaticBody2D named Floor, reporting the first contact.
extends CharacterBody2D
func _physics_process(_delta: float) -> void:
velocity = Vector2(0, 300)
move_and_slide()
for i in get_slide_collision_count():
var collision := get_slide_collision(i)
print("hit ", collision.get_collider().name, ", normal ", collision.get_normal(), ", on floor: ", is_on_floor())
set_physics_process(false)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
hit Floor, normal (0.0, -1.0), on floor: trueVerified on Godot 4.7.2
Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.
Where the check lives in Godot's source: [gdscript_analyzer.cpp line 3767, the message with its hint](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_analyzer.cpp#L3767), [CharacterBody2D.get_slide_collision in the class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/CharacterBody2D.xml).
Questions
- Why is the count 0 when I call it in _process()?
- Collisions are from the last move_and_slide(), which runs in _physics_process(). Read them right after it.
- How do I push a RigidBody2D I walk into?
- Read the collision's collider, check it is a RigidBody2D, and call apply_central_impulse(-collision.get_normal() * force).
Related
- KinematicBody2D in Godot 4: it is CharacterBody2D now
- How to raycast from code in Godot 4
- Every verified Godot 4 answer
- Function "…()" not found in base self in Godot 4
- KinematicBody in Godot 4: it is CharacterBody3D now
- Can Laguna S 2.1 write working Godot 4 code? 9 of 15 on the first try
- Can Nemotron 3.5 Super VL write working Godot 4 code? 11 of 15 on the first try
- Can Grok 4.3 write working Godot 4 code? 11 of 15 on the first try
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free