Godot version
Godot 4.6.3
Question
Hi guys,
Right now, there is an issue where when I jump, the character gets stuck in the air, and only and goes down when I press my up + down keybinds. And also when I press the attack button.
Another issue I noticed was different key binds playing the same animation. Where when I press spacebar to jump, it plays the running animation instead.
Here’s the code:
extends CharacterBody2D
const SPEED = 13000.0
const JUMP_VELOCITY = -300.0
@onready var animation_tree: AnimationTree = $AnimationTree
var attacking = false
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity.y += gravity * delta
if Input.is_action_just_pressed("attack"):
animation_tree.get("parameters/playback").travel("Attack")
attacking = true
if attacking == false:
var input_vector = Vector2(
Input.get_action_strength("forward") - Input.get_action_strength("backward"),
Input.get_action_strength("jump")).normalized()
self.velocity = input_vector * delta * SPEED
if input_vector == Vector2.ZERO:
animation_tree.get("parameters/playback").travel("Idle")
elif Input.is_action_just_pressed("jump") and is_on_floor():
animation_tree.get("parameters/playback").travel("Jump")
animation_tree.set("parameters/Jump/BlendSpace2D/blend_position", input_vector)
velocity.y = JUMP_VELOCITY
else:
animation_tree.get("parameters/playback").travel("Run")
animation_tree.set("parameters/Idle/BlendSpace2D/blend_position", input_vector)
animation_tree.set("parameters/Attack/BlendSpace2D/blend_position", input_vector)
animation_tree.set("parameters/Run/BlendSpace2D/blend_position", input_vector)
move_and_slide()
func _on_animation_tree_animation_finished(animation_tree) -> void:
if "Attack" in animation_tree:
attacking = false
For the wrong animation issue, the problem may be this line here:
animation_tree.get("parameters/playback").travel("Run")
But I could be wrong.