Reputation: 1
I am starting to program a platformer as a way to learn Godot. I wanted to start with making the player movement, first the left, and right then the jumping.
After following a tutorial on YT how to make good jumps: https://youtu.be/IOe1aGY6hXA I encountered the problem that my player doesn't really jumps to the highest position. It seems rather that he is just teleporting to the highest position. I did not find the error but I assume it has something to do with the way I apply the gravity to the player when it he jumps.
The Code:
extends CharacterBody2D
@export var speed = 444.44
@export var jump_height:float
@export var jump_time_to_peak:float
@export var jump_time_to_descent:float
@onready var jump_velocity:float = ((2.0*jump_height)/jump_time_to_peak)*-1.0
@onready var jump_gravity:float = ((-2.0*jump_height)/(jump_time_to_peak*jump_time_to_peak))*-1.0
@onready var fall_gravity:float = ((-2.0*jump_height)/(jump_time_to_descent*jump_time_to_descent))*-1.0
func _physics_process(delta):
#Code for left and right movement
#var input_vector=Vector2.ZERO
#input_vector.x = Input.get_action_strength("right") - Input.get_action_strength("left")
#velocity = input_vector*speed
#if velocity !=Vector2.ZERO:
$beta_character.play_walk()
#else:
$beta_character.play_idle()
if velocity <Vector2.ZERO:
$beta_character/Sprite2D.flip_h = true
else:
$beta_character/Sprite2D.flip_h = false
velocity.y =get_gravity()*delta
if Input.is_action_just_pressed("jump") and is_on_floor():
jump()
move_and_slide()
func get_gravity() -> float:
return jump_gravity if velocity.y < 0.0 else fall_gravity
func jump():
velocity.y = jump_velocity
I tried solving the issue on my own but it didn't work.
Upvotes: 0
Views: 447
Reputation: 1729
You missed the + in the line
velocity.y =get_gravity()*delta
which hard resets your jump after one tick. To be in line with the tutorial it should be
velocity.y += get_gravity()*delta
so your jump impulse gets slowly catched by the gravity to bring the character back down.
This will only work, if you change parts of the code you commented out here however (assuming you want to put that back in). There you reset the velocity.y back to zero every time, which should not be the case. This will not only result in a very slow fall, but also get_gravity() will always return fall_gravity.
velocity = input_vector*speed # since input_vector.y is 0 , velocity.y will be one
This line should only alter velocity.x, because you try to detect left, right movement. So it should look like this:
velocity.x = input_vector.x*speed
So to have the jump work correctly your code would look like this: (commented in the left right code)
extends CharacterBody2D
@export var speed = 444.44
@export var jump_height:float
@export var jump_time_to_peak:float
@export var jump_time_to_descent:float
@onready var jump_velocity:float = ((2.0*jump_height)/jump_time_to_peak)*-1.0
@onready var jump_gravity:float = ((-2.0*jump_height)/(jump_time_to_peak*jump_time_to_peak))*-1.0
@onready var fall_gravity:float = ((-2.0*jump_height)/(jump_time_to_descent*jump_time_to_descent))*-1.0
func _physics_process(delta):
#Code for left and right movement
var input_vector=Vector2.ZERO
input_vector.x = Input.get_action_strength("right") - Input.get_action_strength("left")
velocity.x = input_vector.x*speed
if velocity != Vector2.ZERO: #may want to change this too, since there will be unwanted behaviour in air
$beta_character.play_walk()
else:
$beta_character.play_idle()
if velocity.x < 0: #only x should determine if the sprite looks left or right
$beta_character/Sprite2D.flip_h = true
else:
$beta_character/Sprite2D.flip_h = false
velocity.y +=get_gravity()*delta
if Input.is_action_just_pressed("jump") and is_on_floor():
jump()
move_and_slide()
func get_gravity() -> float:
return jump_gravity if velocity.y < 0.0 else fall_gravity
func jump():
velocity.y = jump_velocity
Upvotes: 0