How to prevent infinite jumping/flying?

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

I’m brand new to making games and code. I’m following a tutorial by GDQuest about making your first 2d game with Godot, but I’ve run into an issue writing the script for the player character.

From what I can tell my code is identical to the video but when I jump, my character just takes off flying and never comes back.

func _physics_process(delta: float) -> void:
   var direction: = get_direction()
   velocity = calculate_move_velocity(velocity, direction, speed)
   velocity = move_and_slide(velocity, FLOOR_NORMAL)

func get_direction() -> Vector2: 
   return Vector2(
	   Input.get_action_strength("move_right") - Input.get_action_strength("move_left"), -1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0
)

func calculate_move_velocity(
	linear_velocity: Vector2, 
	direction: Vector2, 
	speed: Vector2
        ) -> Vector2: 
    var new_velocity : = linear_velocity
    new_velocity.x = speed.x * direction.x
    new_velocity.y += gravity * get_physics_process_delta_time()
    if direction.y == -1.0:
	 new_velocity.y = speed.y * direction.y
    return new_velocity

I came across this question which was answered where the user had the same problem as me, but my issue seems to be caused by something else because as far as I can understand, my code should do the same thing.

I’m still trying to build a fundamental understanding of coding so I apologize for such a simple error.

When do you calculate direction.y?

RedBlueCarrots | 2020-09-10 02:24

it is calculated in get_direction, in the code above

p7f | 2020-09-10 18:39

Hey, i just copied your exact code you provided in a custom kinematicbody2d and it works just fine. I cannot reproduce your problem.

Can you share how you defined variables like speed, direction, gravity, etc?

p7f | 2020-09-10 18:44

I defined them in a separate script:

extends KinematicBody2D
class_name Actor

const FLOOR_NORMAL: = Vector2.UP

export var speed: = Vector2(300.0, 1000.0)
export var gravity : = 800.0

var velocity: = Vector2.ZERO

chunned | 2020-09-10 18:47

with those parameters it also works for me… although it jums really hight because of the 1000 you set in speed.y… but it works fine

p7f | 2020-09-10 18:52

But how are you defining those parameters in other script? the function where you move shouldnt be in the same kinematicbody2d script?

p7f | 2020-09-10 18:53

Ah, that speed.y = 1000 was the error. You’re right, it was working, just jumping really really high. I suppose I never waited long enough to see it fall back down once it jumped off screen. Thanks for helping me find the issue

chunned | 2020-09-10 18:54

Glad yo help! It would be nice if you add an answer and select it, so the question does not remain unanswered. Or i could add the answer myself if you prefer.

p7f | 2020-09-10 18:58

Just added the answer and credited you. In my original post I had left out the first line of the script which implements the 2nd script:

extends Actor

func _physics_process.....

chunned | 2020-09-10 19:03

:bust_in_silhouette: Reply From: chunned

The issue, as pointed out to me by user p7f, was that in my parent script for the actors/characters, I had set speed.y = 1000. This caused the jump to be extremely high and floaty, making me think I was just flying off. When you change speed.y to a lower integer you can see it working properly.

Note: in the code I originally posted I had omitted the first line of the script, which implements the parent script:

extends Actor