I am trying to make gravity for my character, but I can't use is_on_floor()
without calling move_and_slide()
. How do I call move_and_slide()
in GDscript?
Asked
Active
Viewed 592 times
0
-
You should be able to call `is_on_floor()` directly on your object, as long as it's a KinematicBody or KinematicBody2D. Can you post your code? Including the part that says "extends" at the top? – JarWarren Sep 11 '20 at 18:56
-
extends KinematicBody2D var motion = Vector2() var gravity = -10 var speed = 1 var aceleration = 3 func _process(delta): if not is_on_floor(): position.y += gravity if Input.is_action_pressed("ui_right"): position.x += speed if Input.is_action_pressed("ui_left"): position.x += -speed – GoomyBoy Sep 11 '20 at 19:50
1 Answers
0
The is_on_floor()
, is_on_wall()
, is_on_ceiling()
, or get_slide_count()
functions only have context when you are moving the KinematicBody2D with move_and_slide()
or move_and_slide_with_snap()
. If not, they will return false
or 0
because they are never updated correctly.
The Godot documentation explains how to use these functions very well:
Using KinematicBody2D Godot Documentation
The 2d platformer demo is a good reference for implementing these functions in a real world use case (a little more complicated). It uses move_and_slide_with_snap
, but very similar logic applies. Here is the link:

CJay Horton
- 65
- 9