Player not jumping

player is not jumping and here are the codes, the player can move but not jump

extends CharacterBody2D
class_name Player_Controller

@export var speed = 10.0
@export var jump_power = 10.0

var speed_multiplier = 30.0
var jump_multiplier = 30.0
var direction

#const SPEED = 300.0
#const JUMP_VELOCITY = -400.0


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_power * jump_multiplier

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	@warning_ignore("shadowed_variable")
	var direction := Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * speed * speed_multiplier
	else:
		velocity.x = move_toward(velocity.x, 0, speed * speed_multiplier)

	move_and_slide()

Hello! I would recommend to change a couple of things in your code.

Instead of using jump_power * jump_multiplier I would recommend you to set specific variable like this: var jump_velocity : float = 10.
This is not an actual problem in your code, but it’s usually a good practice to have this… if you don’t use some kind of special movement system where player accelerates more and more while he moving.

There is a mistake in your “add the gravity” part when player has jumped and is now in the air.

  1. You are setting scalar value (float value) to the vector variable. velocity += get_gravity() * delta is incorrect because you’re replacing the vector with scalar value.
  2. Instead of “adding” the gravity * delta to the velocity.y you actually should subtract the velocity. Because we’re not jumping. We’re forcing the player character go to down in terms of velocity when he is in the air.
  3. I would also recommend using the get project setting method to get the gravity parameter. That would be more better because you can always call that project setting in any scripts you want without relying on a variable that is located exactly in one specific script.

The resulting code part should look like this:
velocity.y -= ProjectSettings.get_setting(“physics/2d/default_gravity”) * delta
instead of velocity += get_gravity() * delta.

Atleast that’s some things I’ve found in your code. I don’t know exactly why your player character does not jump but perhaps try checking if custom input “jump” is correctly created (it exists and there is space bar key assigned to it).

@elias_lucky
velocity += get_gravity() * delta is absolutely fine. Your suggested code would ignore the gravity direction from the project settings and any gravity overrides from Area2Ds. get_gravity() considers those things and returns a vector that you have to add to the velocity, not substract.

@Sharxxy
Your default jump velocity.y would be 10.0 * 30.0 = 300.0, which is a positive value. For 2d games in Godot, a positive Vector2.y is pointing downwards, not upwards. So you have to make sure velocity.y is set to a negative value for jumping.

1 Like

You’re right, my bad. I haven’t thought that get_gravity() would return Vector2 instead of scalar value.

1 Like