Prevent kinematic body from falling off from edges.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Technipion

I’m using move_and_slide to move my player model (kinematic body) around. I want to prevent it from falling off edges when it’s crouching. Is there a simple way to do that?
The collision shape is a capsule with a cylinder at the bottom (so that the player can slide over small things, but doesn’t slide off edges).


My code:

func _physics_process(delta : float) -> void:
    # stuff
    # more stuff

func process_movement(delta : float) -> void:
    # stuff
    # basically the movement code from the Godot FPS tutorial:
    hvel = hvel.linear_interpolate(target, accel * delta)
    vel.x = hvel.x
    vel.z = hvel.z
    vel = move_and_slide(vel, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
    # some more stuff

Can you show us the code?

Schweini | 2019-04-09 15:22

Thx for your reply. I updated my original question. Do you have any ideas how I could prevent move_and_slide from falling off edges?

Technipion | 2019-04-10 06:56

:bust_in_silhouette: Reply From: Joel_127

I don’t think there is a setup for this. What I would do is use a RayCast node pointing downward from your character front at the angle I would consider a fall. Then use “not $RayCast.is_colliding” when crouching to detect a drop and stop the movement (don’t forget to enable the raycast).

So there is no built-in way of doing that?

Technipion | 2019-04-10 07:00

I don’t think so, the normal physic behavior is to fall from cliffs, you need to do some work to have a different behavior. I’m not an expert though.

Joel_127 | 2019-04-10 07:04

Joel_127 | 2019-04-10 20:33