0 votes

I'm new in Godot.
I go ahead on Udemy tutorial and write below code for bunny game.
when I press Jump key, player has two different Height . I couldn't understand the reason.

extends KinematicBody2D

var Motion  = Vector2(0,0)
var Up      = Vector2(0,-1)
const Jum_Height = 3600
const Gravity = 3600

func _physics_process(delta):
    fFall(delta)
    fMove(delta)
    fHurt()
    fMotion()

func fFall(delta):
    if ! is_on_floor():
        Motion.y += Gravity * delta

func fMove(delta):
    if Input.is_action_pressed("ui_jump"):
        if is_on_floor():
            Motion.y -= Jum_Height

func fMotion():
    move_and_slide(Motion,Up)

many thanks.

in Engine by (27 points)

1 Answer

0 votes
Best answer

The problem is, when you hit the floor, your Motion.y is not reset to 0. Whatever value it had once you hit the floor stays, and since you're subtracting the jump height from that value, the jump height will alternate between two different heights. Also, is_on_floor() only returns true after move_and_slide() is called, so the first frame will apply gravity and give the movement vector a non-zero value from the start.

Change the line

Motion.y -= Jum_Height

to

Motion.y = -Jum_Height

Since you're no longer subtracting the jump height from a non-zero value, you should also reduce your jump height, or the character will jump higher than before.

by (8,546 points)
selected by

many thanks and I got I don't know anything about that

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.