Playing Hit Animation

Godot Version

the newest

Question

Why does my “hit” animation no play when i get hit. It prints the line “you was hit” but the animation didn’t play. My Code is below and this is my very first Project, so if you spot something overcomplicated, let me know how to make things better

thank you

Plays Animation

if fully_healt == true:
	if is_on_floor():
		if direction == 0:
			animated_sprite.play ("idel_cap")
		else: animated_sprite.play("run_cap")
	else:
		animated_sprite.play("jump_cap")
else:
	if is_on_floor():
		if direction == 0:
			animated_sprite.play ("idel_without")
		else: animated_sprite.play("run_without")
	else:
		animated_sprite.play("jump_without")


#Apply movement
if direction:
	velocity.x = direction * SPEED 
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

#check in GM if health is full
func _on_game_manager_full_health():
fully_healt = true
print(“You full!”)

#Dying “Animation”
func _on_game_manager_zero_health():
collision_shape_bird.queue_free()

func _on_game_manager_getting_hit():
animated_sprite.play(“hit”)
fully_healt = false
print(“You was hit”)

Its likely that you are running the hit animation but immediately running idel_cap/idel_without or jump_cap/jump_without.
If the first block of code is running in one of the _process() functions then it is going to run immediately after you get hit and cancel out your hit animation.
You will need to prevent those other animations from playing until your hit animation finishes.

Please post your code using the code tag </>

1 Like

to easily do what @sancho2 said, you can actually check the current playing animation if it’s hit and is_playing(), then dont play other animation

change to this:

if animated_sprite and animated_sprite.animation=="hit" and animated_sprite.is_playing():
	pass
elif fully_healt == true:
	if is_on_floor():
		if direction == 0:
			animated_sprite.play ("idel_cap")
		else: animated_sprite.play("run_cap")
	else:
		animated_sprite.play("jump_cap")
else:
	if is_on_floor():
		if direction == 0:
			animated_sprite.play ("idel_without")
		else: animated_sprite.play("run_without")
	else:
		animated_sprite.play("jump_without")
1 Like

this looks right, but if i try to use this code i get a error saying "Expected indented block after “if” block.
And i don’t get why this happens.

Thankyou!!!

you need to rearrange the tab indentation of each line to make it right, if you dont know how, send the whole code here

1 Like

I hope you get your “hit” animation playing, I was having the same trouble yesterday and just got it working a moment ago.

Needless to say my coding is all-over the place and would probably give an experienced Godot user apoplexy and nowhere as good as I would like, but, it is working, a good first experience.

@export_category("Movement Parameters")
@export var Jump_peak_time: float = 0.3
@export var Jump_fall_time: float = 0.3
@export var Jump_height: float = 2.0
@export var Jump_distance: float = 4.0


@export var Coyote_time:float = 0.1
@export var Jump_buffer_timer: float = 0.1

var speed: float = 5.0
var jump_velocity: float = 5.0
	#recalculate is to pixel
var pixel_jump_height:float = (16*Jump_height)
var pixel_jump_distance: float = (16*Jump_distance)






#New jump physics functions
func _ready() -> void:
	calculate_movement_parameters()

func calculate_movement_parameters() -> void:
	jump_gravity = (2*pixel_jump_height)/pow(Jump_peak_time,2)
	fall_gravity = (2*pixel_jump_height)/pow(Jump_fall_time,2)
	jump_velocity = jump_gravity * Jump_peak_time
	speed = pixel_jump_distance/(Jump_peak_time+Jump_fall_time)
	print ("distance: " + str(pixel_jump_distance))
	print("hight: " + str(pixel_jump_height))


var fully_healt: bool = false

# Get the gravity from the project settings to be synced with RigidBody nodes.
var jump_gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var fall_gravity:float = 5.0


#Coyote Time
var jump_available: bool = true
func coyote_timeout():
	jump_available = false
	
#Jump Buffer
var jump_buffer = false
func jump() -> void:
		velocity.y -= jump_velocity
		jump_available = false
		jump_sound.play()
func on_jump_buffer_timeout() -> void:
	jump_buffer = false



func _physics_process(delta: float):
	# Add the gravity.
	if not is_on_floor():
		if jump_available:
			get_tree().create_timer(Coyote_time).timeout.connect(coyote_timeout)
		
		if velocity.y>0:
			velocity.y += jump_gravity * delta
		else:
			velocity.y += fall_gravity * delta
		
	else:
		jump_available = true
		if jump_buffer:
			jump()
			jump_buffer = false

	# Handle jump.
	if Input.is_action_just_pressed("move_jump"): 
		if jump_available:
			jump()
		else:
			jump_buffer = true
			get_tree().create_timer(Jump_buffer_timer).timeout.connect(on_jump_buffer_timeout)
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * speed
	else:
		velocity.x = move_toward(velocity.x, 0, speed)


	move_and_slide()

#Flip the Sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true

# Plays Animation
	if fully_healt == true:
		if is_on_floor():
			if direction == 0:
				animated_sprite.play ("idel_cap")
			else: animated_sprite.play("run_cap")
		else:
			animated_sprite.play("jump_cap")
	else:
		if is_on_floor():
			if direction == 0:
				animated_sprite.play ("idel_without")
			else: animated_sprite.play("run_without")
		else:
			animated_sprite.play("jump_without")

#check in GM if health is full
func _on_game_manager_full_health():
	fully_healt = true
	print("You full!")
#Dying "Animation"
func _on_game_manager_zero_health():
	collision_shape_bird.queue_free()

func _on_game_manager_getting_hit():
	animated_sprite.play("hit")
	fully_healt = false
	print("You was hit")

So this is my total Code for my player, i just send everythink if you see something stupid pls let my know, i wanna improve

@export_category("Movement Parameters")
@export var Jump_peak_time: float = 0.3
@export var Jump_fall_time: float = 0.3
@export var Jump_height: float = 2.0
@export var Jump_distance: float = 4.0


@export var Coyote_time:float = 0.1
@export var Jump_buffer_timer: float = 0.1

var speed: float = 5.0
var jump_velocity: float = 5.0
	#recalculate is to pixel
var pixel_jump_height:float = (16*Jump_height)
var pixel_jump_distance: float = (16*Jump_distance)






#New jump physics functions
func _ready() -> void:
	calculate_movement_parameters()

func calculate_movement_parameters() -> void:
	jump_gravity = (2*pixel_jump_height)/pow(Jump_peak_time,2)
	fall_gravity = (2*pixel_jump_height)/pow(Jump_fall_time,2)
	jump_velocity = jump_gravity * Jump_peak_time
	speed = pixel_jump_distance/(Jump_peak_time+Jump_fall_time)
	print ("distance: " + str(pixel_jump_distance))
	print("hight: " + str(pixel_jump_height))


var fully_healt: bool = false

# Get the gravity from the project settings to be synced with RigidBody nodes.
var jump_gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var fall_gravity:float = 5.0


#Coyote Time
var jump_available: bool = true
func coyote_timeout():
	jump_available = false
	
#Jump Buffer
var jump_buffer = false
func jump() -> void:
		velocity.y -= jump_velocity
		jump_available = false
		jump_sound.play()
func on_jump_buffer_timeout() -> void:
	jump_buffer = false



func _physics_process(delta: float):
	# Add the gravity.
	if not is_on_floor():
		if jump_available:
			get_tree().create_timer(Coyote_time).timeout.connect(coyote_timeout)
		
		if velocity.y>0:
			velocity.y += jump_gravity * delta
		else:
			velocity.y += fall_gravity * delta
		
	else:
		jump_available = true
		if jump_buffer:
			jump()
			jump_buffer = false

	# Handle jump.
	if Input.is_action_just_pressed("move_jump"): 
		if jump_available:
			jump()
		else:
			jump_buffer = true
			get_tree().create_timer(Jump_buffer_timer).timeout.connect(on_jump_buffer_timeout)
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * speed
	else:
		velocity.x = move_toward(velocity.x, 0, speed)


	move_and_slide()

#Flip the Sprite
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true

# Plays Animation
	if animated_sprite and animated_sprite.animation=="hit" and animated_sprite.is_playing():
		pass
	elif fully_healt == true:
		if is_on_floor():
			if direction == 0:
				animated_sprite.play ("idel_cap")
			else: animated_sprite.play("run_cap")
		else:
			animated_sprite.play("jump_cap")
	else:
		if is_on_floor():
			if direction == 0:
				animated_sprite.play ("idel_without")
			else: animated_sprite.play("run_without")
		else:
			animated_sprite.play("jump_without")

#check in GM if health is full
func _on_game_manager_full_health():
	fully_healt = true
	print("You full!")
#Dying "Animation"
func _on_game_manager_zero_health():
	collision_shape_bird.queue_free()

func _on_game_manager_getting_hit():
	animated_sprite.play("hit")
	fully_healt = false
	print("You was hit")
1 Like

Cool - you used ‘coyote time’ :grinning:. I am thinking about using that in my game as well. I am just terrible at timing jumps.

1 Like