tow difference height jump

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hossein Vatani

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.

:bust_in_silhouette: Reply From: exuin

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.

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

Hossein Vatani | 2020-09-28 12:47