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()