Godot Version
v4.3.stable.official [77dcf97d8]
Question
Hi Forum!!
So below is the code I have for my 2d beat em up game like Street of Rage 4!
Now I have the jumping working but when holding left and up (to walk left upwards at the same time) and then press jump the character never jumps. Now if I hold right and up (to walk right and upwards at the same time) it works the character jumps. If I hold left and jump works, If I hold up and jump it jumps, etc. Is just only when holding left and up at the same time that the player is not jumping.
# Variables
@export var speed := 250
@export var jump_height := 200 # Adjust this value to increase jump height
@export var jump_duration := 0.5 # Total time to reach peak and come back down
@onready var adam_animated_sprite = $AnimatedSprite2D
@export var jabbing := false
@export var uppercutting := false
@export var is_jumping := false
var target_velocity = Vector2.ZERO # Used for smoothing velocity
var jump_timer = 0.0 # Timer to track jump progress
var start_y_position = 0.0 # The Y position before jump starts
# This code controls the up, down, right, and left movement of the Character.
@warning_ignore("unused_parameter")
func _process(delta: float) -> void:
var direction = Vector2.ZERO # Define direction here so it’s always in scope
# Check if jab is triggered and set jabbing to true
if Input.is_action_just_pressed("Jab") and not jabbing:
jab()
# Prevent movement if jabbing
if jabbing:
velocity = Vector2.ZERO # Stop movement when jabbing
else:
# Prevent movement while jumping
if not is_jumping:
direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
# Use lerp to smoothly change the velocity
target_velocity = direction * speed
velocity = velocity.lerp(target_velocity, 0.3)
# Flip sprite based on direction
if direction.x != 0:
adam_animated_sprite.flip_h = direction.x < 0
# Handle jump input
if Input.is_action_just_pressed("ui_select") and not is_jumping:
jump()
# Handle jumping movement
if is_jumping:
jump_timer += delta
var jump_progress = jump_timer / jump_duration
# Apply the jump arc
if jump_progress < 1.0:
# Move up, then down by adjusting Y position based on jump progress
position.y = start_y_position - jump_height * sin(jump_progress * PI)
else:
# End the jump
is_jumping = false
position.y = start_y_position # Reset to ground level
# Apply the updated velocity to move the character only if not jabbing
if not jabbing:
move_and_slide()
play_animations(direction)
# This code controls the animations when jabbing, jumping, walking, and idle.
func play_animations(dir):
if jabbing:
adam_animated_sprite.play("Jab")
elif is_jumping:
adam_animated_sprite.play("Jumping")
else:
if dir.length() > 0:
adam_animated_sprite.play("Walking")
else:
adam_animated_sprite.play("Idle")
# Function to handle jabbing action
func jab():
jabbing = true
adam_animated_sprite.play("Jab")
await adam_animated_sprite.animation_finished # Wait for the jab animation to finish
jabbing = false
# Jump function to start the jump
func jump():
is_jumping = true
jump_timer = 0.0
start_y_position = position.y # Record the ground level Y position before jump
I did had a print to whenever we press jump! It prints isJumping on all the other directions but when holding left and up at the same time and pressing jump it does not print isJumping. Any Ideas been looking at the code but I got no Idea why!!!