Player flies when walking to edges using slide movements in the 3D world

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

I used Godot 3.1

I don’t understand why the 3D kinematic controls have different sliding system to the 2D kinematic controls when I try to code similarly to the 2D controls I had. The problem isn’t visible if I don’t use any collision shapes that has curved bottom or top like cylinder, box. If I use a capsule or maybe a sphere I would fly off when I walk to an edge or let’s say a stair or something and this problem only happens in the 3D world but not in 2D I checked up on it. I’ll show the code I wrote in the _physics_process the basic movements for the player in 3D.

extends KinematicBody

var velocity = Vector3(0,0,0)
export var SPEED : float = 8
const UP = Vector3(0,1,0)


func _physics_process(delta):

var g = Vector3(0,-9.8,0)

velocity += g * delta

if Input.is_action_pressed("right") and Input.is_action_pressed("left"):
	velocity.x = 0
elif Input.is_action_pressed("right"):
	velocity.x = SPEED
elif Input.is_action_pressed("left"):
	velocity.x = -SPEED
else:
	velocity.x = lerp(velocity.x,0,.75)

if Input.is_action_pressed("forward") and Input.is_action_pressed("backward"):
	velocity.z = 0
elif Input.is_action_pressed("backward"):
	velocity.z = SPEED
elif Input.is_action_pressed("forward"):
	
	velocity.z = -SPEED
else:
	velocity.z = lerp(velocity.z,0,.75)

velocity = move_and_slide(velocity, UP)

pass

Here is a gif where I walk to an edge and player flies I didn’t jump I didn’t include a jump control on it.
enter image description here

I don’t want want the player to slide and fly off from the edge like that. Since I used move_and_slide only to the 3D, the code must have applied something to slide and fly when walking to an edge. I’m not sure about it yet but I want to know what’s going on.

:bust_in_silhouette: Reply From: Dlean Jeans

It behaves quite realistically, exactly what I would expect in real life.
Here’s a few things you can try:

  • Add a PhysicsMaterial and increase the friction (and experiment with the other properties).

  • Use move_and_collide instead of move_and_slide (it has “slide” in the name, you should expect it to slide)

  • Increase the gravity

:bust_in_silhouette: Reply From: Dlean Jeans

move_and_slide got a parameter called stop_on_slope if you set it to true, it will still slide but not fly:

velocity = move_and_slide(velocity, UP, true)

I already did that but if you walk to the tip of an edge you would fly off.

KramchayDig | 2019-07-04 12:40