Move_and_slide returning false while player is in motion

4.1.3

Question

I’m new to Godot so I might just be misunderstanding how things are supposed to work, but when my player is moving horizontally across the ground move_and_slide() is returning false (no collisions). When the player stops, it registers the collision with the tilemap. Shouldn’t it register the collision with the tilemap as it slides across it? Is there a way to get it to register the collision while still in motion?

``````func player_movement(delta):
var x_val = int(Input.is_action_pressed("MoveRight")) - int(Input.is_action_pressed("MoveLeft"))

if not is_on_floor():
velocity.y += gravity * delta

if Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = -400

if (x_val == 0):
if (abs(velocity.x) > friction * delta):
if (velocity.x > 0):
velocity.x -= (friction * delta)
else:
velocity.x += (friction * delta)
else:
velocity.x = 0
else:
velocity.x += (x_val * ACCEL * delta)
if (velocity.x > 0 and velocity.x > MAX_SPEED):
velocity.x = MAX_SPEED
elif (velocity.x < 0 and velocity.x < -MAX_SPEED):
velocity.x = -MAX_SPEED

collision = move_and_slide() # returns false if the player is in motion

func _physics_process(delta):
# Other code here to get the collider and collision layer

player_movement(delta)
``````

From the docs:

Modifies velocity if a slide collision occurred. To get the latest collision call `get_last_slide_collision`, for detailed information about collisions that occurred, use `get_slide_collision`.

Maybe try those methods (on your CB2D) to check for collisions? I haven’t used it this way yet so just guessing.

if you just want to check if it’s collided with the floor, then can just use `is_on_floor()`

@ zdrmlpzdrmlp unfortunately I need to the actual collisions to get custom data from each tile as the player passes over it, so is_on_floor() doesn’t quite work

@ alextheukrainian I tried using get_slide_collision_count(), but it returns 0 while the player is in motion.

What does the game look like? (top-down, side scroller, etc.)

Have you googled get_slide_collision_count() etc to see how they’re meant to be used? May be some settings somewhere that leads you to a solution.

Finally, why not just detect yourself? Especially if it’s a top-down game it should be trivial to check which tiles player is on top of. I haven’t worked with tilesets specifically but you can check for visual intersection with any obj.

@ alextheukrainian. It’s a side scrolling platformer. Looking at the documentation is does look like it states that it “only counts times the body has collided and changed direction.”. However that did lead me down another Google rabbit hole where I could get it to do what I wanted with a RayCast2D Node, so thank you very, very much.

1 Like

Awesome. It seems every project sooner or later is forced to use a raycast >.<

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.