Godot Version 4
Question
Hi everyone! I’m (Total beginner) creating a 2D Platformer. The character picks up a weapon and can attack (ui_Attack).
But when I press the Attack Button while also pressing the Jump button (ui_accept") or the left,right (ui_left / ui_right) button, my jumps become way to high or too far.
I tried to find the mistake but was unable to. I know it might be my _physiscs_process function but i’m stuck. Maybe you can help me. Thanks in advance! (English is not my main language, sorry in advance for any mistakes :D)
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
onready var animSprite = $AnimatedSprite2D
var hasSword = false
var isAttacking = false
var toggleAttack = false
func checkAttack(): # Test, ob Angriff erfolgt
if Input.is_action_just_pressed(“ui_Attack”) :
isAttacking = true
$AttackArea/CollisionShape2D.disabled = false
randomize()
var randomAttack = randi\_range(1,3)
match(randomAttack):
1: animSprite.play("Attack1")
2: animSprite.play("Attack2")
3: animSprite.play("Attack3")
if(animSprite.flip\_h == true):
$AttackArea/CollisionShape2D.position.x \*= -1
toggleAttack = true
func _physics_process(delta):
var swordExtra =“_nos”
if(hasSword):
swordExtra ="\_hs"
if(isAttacking == false):
if not is\_on\_floor():
velocity.y += gravity \* delta
if (velocity.y > 0): # Wenn wir fallen
animSprite.play(“Fall” + swordExtra)
else:
animSprite.play(“Jump” + swordExtra)
else:
if(velocity.x == 0):
animSprite.play(“Idle” + swordExtra)
else:
animSprite.play(“Run” + swordExtra)
# Handle jump.
if Input.is\_action\_just\_pressed("ui\_accept") and is\_on\_floor():
velocity.y = JUMP\_VELOCITY
var direction = Input.get\_axis("ui\_left", "ui\_right")
if direction:
velocity.x = direction \* SPEED
if(velocity.x < 0):
animSprite.flip_h = true
else:
animSprite.flip_h = false
else:
velocity.x = move\_toward(velocity.x, 0, SPEED)
if(hasSword):
checkAttack()
move_and_slide()
func _on_animated_sprite_2d_animation_finished():
isAttacking = false
$AttackArea/CollisionShape2D.disabled = true
if (toggleAttack == true):
$AttackArea/CollisionShape2D.position.x \*= -1
toggleAttack = false