extends CharacterBody2D @onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D @onready var coyote_timer: Timer = $CoyoteTimer @onready var jump_buffer_timer: Timer = $JumpBufferTimer
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var Jump_Available:bool = true
var Jump_Buffer:bool = false
var jumpcount = 0
var is_attacking = false
func _physics_process(delta: float) → void:
# Get the input direction and handle the movement/deceleration.
var direction := Input.get_axis(“move_left”, “move_right”) #movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if is_on_floor() or Jump_Available:
Jump_Available = true
if Jump_Available:
if Input.is_action_just_pressed("jump"):
velocity.y = JUMP_VELOCITY
jumpcount += 1
if jumpcount == 2:
Jump_Available = false
jumpcount = 0
#Flip sprite
if direction > 0:
animated_sprite.flip_h = false
if direction < 0:
animated_sprite.flip_h = true
#Play animations
if Input.is_action_just_pressed("attack"):
is_attacking = true
if is_attacking:
animated_sprite.play("attack")
if is_on_floor():
if direction == 0:
if is_attacking == false:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
if not is_on_floor():
animated_sprite.play("jump")
move_and_slide()
found out how to make code look better but still need help with solution
extends CharacterBody2D
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var coyote_timer: Timer = $CoyoteTimer
@onready var jump_buffer_timer: Timer = $JumpBufferTimer
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var Jump_Available:bool = true
var Jump_Buffer:bool = false
var jumpcount = 0
var is_attacking = false
func _physics_process(delta: float) -> void:
# Get the input direction and handle the movement/deceleration.
var direction := Input.get_axis("move_left", "move_right")
#movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if is_on_floor() or Jump_Available:
Jump_Available = true
if Jump_Available:
if Input.is_action_just_pressed("jump"):
velocity.y = JUMP_VELOCITY
jumpcount += 1
if jumpcount == 2:
Jump_Available = false
jumpcount = 0
#Flip sprite
if direction > 0:
animated_sprite.flip_h = false
if direction < 0:
animated_sprite.flip_h = true
#Play animations
if Input.is_action_just_pressed("attack"):
is_attacking = true
if is_attacking:
animated_sprite.play("attack")
if is_on_floor():
if direction == 0:
if is_attacking == false:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
if not is_on_floor():
animated_sprite.play("jump")
move_and_slide()
func _on_animated_sprite_2d_animation_finished() -> void:
if $AnimatedSprite2D.animation == "attack":
print("a")
is_attacking = false
Thank you for formatting, sorry it’s hard to find out [code] or ``` blocks.
So it never prints “a”? It seems like the “run” animation is most likely to interrupt the “attack” animation, to debug could you add a print($AnimatedSprite2D.animation) at the start of the connected function?
Hey thanks for the reply, to be honesty I’m not sure how functions work in this 100%. I thought after defining a function you would have to call it to do something but the guy in this youtube video has it working for him at 6:45
and yea I thought the run animation would interfere a little bit but I thought if it goes in the function it would have to at least set is_attacking to false. But just to make sure I removed else condition that plays the run animation and my character keeps looping the attack animation once I attack because is_attacking is never set to false
I have tried putting _on_animated_sprite_2d_animation_finished() right above move_and_slide and it does go into the function where is_attacking = false but the problem is that the attack animation plays for a split second then stops
and even if it did work id still be confused why what the guy did in the video wouldn’t work for me.
I really like godot and it seems pretty straight forward with a lot of things but do you think its be a good idea to go to unity just for the sole reason of there being a lot of tutorials or videos about common problems?
Game engines will behave similarly, so experience in any and all is valuable, you should spend some time with Unity in your lifetime. However, I feel Unity has a lot more inexplicable bugs to dance around; there is even a website to document such, named The Uninomicon. Godot does rather well in documented and reasonable behaviour in my opinion, so it’s a great starting point even if video tutorials are lacking and changing (with 4.0 and 4.3 being so recent).
Personally I do not use video tutorials, text documenation/blogs always served me better. Godot has built-in F1 documentation that will contain links to tutorials if available. I find that a big selling point, especially when working offline.
I am rambling about my favorite engine now, I am glad you found the solution! Despite everything I said, Godot’s UI contains a ton of ambiguous buttons, I didn’t find out about autoplay until a year in! I could see video tutorials or work-streams helping with that discovery.