0 votes

Hi all, Harshdeep here,
I'm totally new in game development side. I followed BornCG in Youtube and made exactly same 3D game. But, now I want to implement some jumping function with disable on gravity. ( means user can't jump while in the air. ). I tried but, failed to implement it.

My controller code.

extends KinematicBody

var velocity = Vector3(0,0,0)

const SPEED = 5
const ROTATE = 5

func _ready():
    #Game Background Music
    var BackgroundMusic = get_node("BackgroundMusic/AudioStreamPlayer3D")
    BackgroundMusic.play()
    #pass

func _physics_process(delta):


    #RollerBall(Player) Control.
    if Input.is_action_pressed("ui_right") and Input.is_action_pressed("ui_left"):
        velocity.x = 0
    elif Input.is_action_pressed("ui_right"):
        velocity.x = SPEED
        $MeshInstance.rotate_z(deg2rad(-ROTATE))
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -SPEED
        $MeshInstance.rotate_z(deg2rad(ROTATE))
    else:
        velocity.x = lerp(velocity.x,0,0.1)

    if Input.is_action_pressed("ui_up") and Input.is_action_pressed("ui_down"):
        velocity.z = 0
    elif Input.is_action_pressed("ui_up"):
        velocity.z = -SPEED
        $MeshInstance.rotate_x(deg2rad(-ROTATE))
    elif Input.is_action_pressed("ui_down"):
        velocity.z = SPEED
        $MeshInstance.rotate_x(deg2rad(ROTATE))
    else:
        velocity.z = lerp(velocity.z,0,0.1)

    move_and_slide(velocity)
asked Aug 5 in Engine by Harshdeep Patel (17 points)

1 Answer

+1 vote
Best answer

If you pass move_and_slide() a floor_normal value, you can use is_on_floor() to determine if you're standing on the ground or not, and allow a jump. You should never disable gravity - it's a constant force pulling downward. To jump, set your body's y velocity to the desired value.

For an example, see: https://docs.godotengine.org/en/latest/tutorials/physics/using_kinematic_body_2d.html

(It's 2D, but the concept is the same in 3D)

More information can be found in the KinematicBody documentation.

answered Aug 5 by kidscancode (11,756 points)
selected Aug 6 by Harshdeep Patel

thanks brother. Just explored your YT channel. and changed entire code.
thanks a lot. you got one more subscriber. :)

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.