0 votes

I made a kinematicbody that has a capsule collision shape and capsule mesh. I'm trying to apply gravity to it and I succeeded somewhat. The problem is that the capsule slightly sinks into the floor.

I've set moveandslide's up direction to Vector3.UP and I even made a print("player is on ground") to check if isonfloor is true. Each time I check it says that isonfloor is true. So it knows it is on the ground, but sinks into it!

my 3dmovement function which has the downward force is:

func movement_handler(delta):
var direction = Vector3()
if Input.is_action_pressed("move_right") and Input.is_action_pressed("move_left"):
    direction.x = 0
elif Input.is_action_pressed("move_right"):
    direction += transform.basis.x
elif Input.is_action_pressed("move_left"):
    direction -= transform.basis.x

if Input.is_action_pressed("move_forward") and Input.is_action_pressed("move_backward"):
    direction = 0
elif Input.is_action_pressed("move_backward"):
    direction += transform.basis.z
elif Input.is_action_pressed("move_forward"):
    direction -= transform.basis.z

if Input.is_action_pressed("sprint"):
    direction = direction.normalized() * run_speed
else:
    direction = direction.normalized() * walk_speed

if is_on_floor():
    direction.y = -0.01
else:
    direction.y -= gravity

if Input.is_key_pressed(KEY_H) and is_on_floor():
    print("Player is on the ground.")

move_and_slide(direction, Vector3.UP)

and I call this function in the physicsprocess(delta) function

Godot version v3.4-stable
in Engine by (12 points)

1 Answer

0 votes

Perhaps Your player is not on_floor every few frames in process() and it is enough to make him be pressed below ground with gravity. You will not see it by printing in process(), You could eventually print in the line where gravity is applied - how often that happens when player is idle.

Anyhow, You are forcing body on the ground with code. Is there any reason You can't make it collide with the floor ?

by (5,128 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.