Search code examples
godotgdscript

When is_on_floor() returns true the player can't move


This is the code, https://github.com/Gioroccovanni/SteamP2P.

func _physics_process(delta):
    if not is_multiplayer_authority(): return
    
    if !is_on_floor():
        velocity.y-=gravity*delta
    
    var input_dir = Input.get_vector("left", "right", "forward", "backward") #tutte le direzioni diverse da forward sono rotte :c
    var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y))#.normalized()
    
    if direction:
        velocity.x = direction.x * SPEED
        velocity.z = direction.z * SPEED
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
        velocity.z = move_toward(velocity.z, 0, SPEED)
        
    print(is_on_floor())
    
    move_and_slide()

After trying implementing gravity (player.gd lines 27 and 28) the player falls into place(while falling it can move) but as soon as it hits the ground it can't move anymore.

Tried tuning up the speed and it doesn't affect it, when printing the velocity it actually changes with input, but doesn't seem to move the character.


Solution

  • Ok, I have reviewed the project.

    Aside from some details (notably I had to remove the WorldEnvironment and add a new one to have any performance), this is the big no-no: Do not use CSG with collisions inside of other physics bodies.

    The player character has a CSG object inside of it that has collision enabled. This CSG object will collide with the ground, and the player will collide with it, getting stuck.

    So, do not do that. Instead give the player CollisionShape3D children for the collisions (it already has one) and MeshInstance3D for the graphics.