My code is wrong somewhere my Main character will move but not jump

Godot 4 latest version

I have tested my collision and it works the only problem is while I can move left and right and the animations work I can not jump which leads me to believe its in my code. Any and all help is greatly appreciated.

here’s my code:

extends CharacterBody2D

Declare variables for movement speed, jump force, and gravity

var speed = 200 # Movement speed (pixels per second)
var jump_speed = -600 # Jump force (negative value to go up)
var gravity = 1000 # Gravity strength (positive value to pull down)

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
# Handle horizontal movement
var direction = Vector2.ZERO # Reset direction every frame

if Input.is_action_pressed("ui_right"):
	direction.x += 1
if Input.is_action_pressed("ui_left"):
	direction.x -= 1

# Normalize direction to prevent faster diagonal movement
direction = direction.normalized()

# Set horizontal velocity
velocity.x = direction.x * speed

# Handle vertical velocity (gravity and jumping)
if is_on_floor():  # If the player is on the ground
	if Input.is_action_just_pressed("ui_jump"):  # Jump input
		velocity.y = jump_speed  # Apply jump force
else:
	velocity.y += gravity * delta  # Apply gravity when not on the ground

# Move the character using move_and_slide (without arguments)
move_and_slide()
1 Like

Hey there, welcome to the forums.
When you’re asking for help and you include a code example, please select your code while editing your post, and click on the “Preformatted text” icon to format it so others can read it properly and help you out!

image

ui_jump isn’t a default action, have you made your own actions in the Project Settings → Input Map?

One other thing that you might want to check is if your jump speed is negative or positive. In Godot, negative y is up and positive y is down, so if you’re setting the jump velocity to a positive number in your code, you’re pushing the object into the ground instead of sending the object up.

I Have it so the character should move up or -y but I tried the same code on another project and it worked