+2 votes

Im want to make a 3D FPS game in Godot 3.4.4 and i have problems with jumping my code ↓↓↓

extends KinematicBody

export var speed = 11
export var accel = 5
export var gravity = 0.98
export var jump_power = 30

onready var head = $Head
onready var camera = $Head/Camera

var velocity = Vector3()

func _physics_process(delta):
    var head_basis = head.get_global_transform().basis

    var direction = Vector3()
    if Input.is_action_pressed("ui_up"):
        direction -= head_basis.z
    elif Input.is_action_pressed("ui_down"):
        direction += head_basis.z

    if Input.is_action_pressed("ui_left"):
        direction -= head_basis.x
    elif Input.is_action_pressed("ui_right"):
        direction += head_basis.x

    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y += jump_power

    direction = direction.normalized()

    velocity = velocity.linear_interpolate(direction * speed, accel * delta)
    velocity.y -= gravity

    velocity = move_and_slide(velocity)

if me printing is on floor()
and im reciving false, why?
P.S.:I always write velocity.y - = gravity because I read other answers and it said: "You always need to move the player a little down"

in Engine by (14 points)

1 Answer

+5 votes

I think this might be happening because godot can't really tell what is a floor or what is a wall or what is a ceiling. To fix this in the move and slide function after velocity add Vector3.Up. It will look like this:

velocity = move_and_slide(velocity, Vector3.UP)

This tells godot that you are above a floor and like that it can detect wall and ceiling
If we did the opposite like Vector3.DOWN Then godot will treat the actual floor as ceiling
And the ceiling as floor

Hope this helps :)

by (73 points)
edited by

Just wanted to add. I believe the colliding bodies are constantly pushing each other away that causes the isonfloor() function to return false.
That's why we need a constant force "gravity" to keep them together.
I hope my logic is correct.

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 Frequently asked questions and 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 [email protected] with your username.