+1 vote

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")

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
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -SPEED
        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
    elif Input.is_action_pressed("ui_down"):
        velocity.z = SPEED
        velocity.z = lerp(velocity.z,0,0.1)

in Engine by (18 points)

2 Answers

+3 votes
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.

by (22,067 points)
selected by

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

–1 vote

const Jumpgravity = 9.8
const jump
power = 12

#"ui_jumping" is created from (project setting>input map>Action>You can create keys )

if is_on_wall() and Input.is_action_just_pressed("ui_jump"):#for Jumping "space".
    velocity.y = jump_power*Jump_gravity
elif is_on_wall()==false or Input.is_action_just_released("ui_jump"):
    velocity.y = -(Jump_gravity*jump_power*delta*2)

#delta is used for increasing the speed in asending order.

by (12 points)
edited by
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.