This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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.

in Engine by (20 points)

When do you calculate direction.y?

it is calculated in get_direction, in the code above

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?

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

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

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

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

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.

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.....

1 Answer

0 votes
Best answer

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