Jump height is related to speed when it shouldn't, which causes the character to jump really high

Godot Version

4.3

Question

My jump height is directly related to the speed I think I know what’s causing it but I have no idea how to fix it

Code:

extends CharacterBody2D 

const speed = 100
@export var acc = 1700
@export var dacc = 1300
var skill = 0
var xp = 0
var max_xp = 500
@export var jump_height : float 
@export var jump_time_to_peak : float
@export var jump_time_to_descent : float
# float variables are variables that can interact with othe objects
@onready var jump_velocity : float = ((2 * jump_height) / jump_time_to_peak) * -1
@onready var jump_gravity : float = ((-2 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1
@onready var fall_gravity : float = ((-2 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1
@onready var jump_buffer_timer = $jump_buffer_timer
@onready var coyote_timer = $coyote_timer
var buffered_jump = false
var is_jumping = false
var player_health = 100
var last_dir = 0
func _physics_process(delta):
	if !is_on_floor():
		velocity.y += GetGravity()*delta
	
	var input_dir: Vector2 = input()
	#movement
	if input_dir != Vector2.ZERO:
		velocity = velocity.move_toward(speed * input_dir, acc*delta) #moves the velocity towards speed*direction(-1 or 1) by the accelaration*delta
		#play_animation() (moving animation)
	else:
		velocity = velocity.move_toward(Vector2.ZERO, dacc*delta) #slows the player down towards 0 by the deacceleration*delta, removing delta breaks gravity
		#play_animation() (idle)
	
	
	if Input.is_action_just_pressed("jump"):
		buffered_jump = true
		jump_buffer_timer.start() #jumps when you reach the floor if you clicked jump before touching the floor by starting a timer that if you land within it will jump
	
	if Input.is_action_just_pressed("jump") or buffered_jump == true: #jumps if space is pressed or space was pressed early
		jump()
	
	if Input.is_action_just_released("jump") and velocity.y < 0:
		velocity.y = velocity.y * 0.2 #if you let go of jump early you jump lower does this by multiplying the y velocity by 0.2 when space is released if the character is moving upwards
	
	var was_on_floor = is_on_floor()#if the character was on the floor before checking
	
	move_and_slide()
	
	if was_on_floor and !is_on_floor():
		coyote_timer.start() #starts the coyote timer if the player has moved from being on the floor to off the floor and if the player jumps within the timespan of the timer it will jump even though player is no longer on the floor
		
	if $Down.is_colliding():
		velocity.y -= 400
		
	if $Left.is_colliding():
		velocity.x += 300
		
	if $Right.is_colliding():
		velocity.x -= 300
		
	
	

func input() -> Vector2: #caculates the input direction by checking which button is being pressed
	var input_dir = Vector2.ZERO
	
	input_dir.x = Input.get_axis("left", "right") # 1 if right -1 if left
	input_dir = input_dir.normalized()
	return input_dir

func GetGravity() -> float: #determines whether the character should be falling or jumping by checking if the character is going upwards or not
	if velocity.y < 0.0:
		return jump_gravity
	if is_on_ceiling() == true:
		return fall_gravity
	else:
		return fall_gravity

func jump(): #if jump is pressed and the character is on the floor the y velocity equals the velocity of the jump
	if is_on_floor() or !coyote_timer.is_stopped() and !velocity.y < 0:
		velocity.y = jump_velocity

func _on_jump_buffer_timer_timeout() -> void:
		buffered_jump = false

My suspicions is that this part is causing it:

var input_dir: Vector2 = input()
	#movement
	if input_dir != Vector2.ZERO:
		velocity = velocity.move_toward(speed * input_dir, acc*delta) #moves the velocity towards speed*direction(-1 or 1) by the accelaration*delta
		#play_animation() (moving animation)
	else:
		velocity = velocity.move_toward(Vector2.ZERO, dacc*delta) #slows the player down towards 0 by the deacceleration*delta, removing delta breaks gravity
		#play_animation() (idle)

You are correct.

velocity is a Vector2 with the x component for left/right and the y component for up/down. When using .move_toward it is affecting both the x and y component. I believe you only want to affect the x component.

velocity.x = move_toward(velocity.x, speed * input_dir, acc*delta)
1 Like

The only issue is that I get the error:

Invalid argument for “move_toward()” function: argument 2 should be “float” but is “Vector2”.

I’m assumed I could fix it by changing it to float.zero though but then I get the error that it doesn’t have a .Zero thing, and it still has that error.

I think that changing the variable type would fix it which it does if i do it for all the variables. The main issue is that it’s telling me to make argument 2 a float which is possible but then when I’m trying to set it my input () function returns a vector 2 and when i change it all to float it should work except for the vector2.ZERO, so is there an equivalent for float?

Sorry, yeah your input_dir should also be a float.

var input_dir: float = Input.get_axis("left", "right")
# Do not normalize input

if input_dir != 0.0:
	velocity.x = move_toward(velocity.x, speed * input_dir, acc*delta)
else:
	velocity.x = move_toward(velocity.x, 0.0, dacc*delta)
1 Like