Problem where animation doesn't run until player leaves the collision box

I’m coding a game where a player atacks an enemy and vice versa, right now i have got the enemy to carry out the attack animation when its collision box collides with the player. But the animation only runs when the player leaves the collision box of the enemy, also after it carrys out the attack animation it stays as a still image when it should carry out a walking animation as it moves again

code that causes the collision function to run

if message_id == msg_collision_response then
		if message.group == group_player then
			handle_player_contact(self, message.normal, message.distance)
		end
	end

the collision function

local function handle_player_contact(self)
	player_contact = true
	self.velocity = vmath.vector3(0,0,0)
	msg.post("#sprite", "play_animation", {id = anim_attack})
	play_animation(self, anim_walk)
end

the animation function

local function play_animation(self, anim)
	if self.anim ~= anim then --this if statement is used to ensure that the function only runs if a new animation is triggered
		msg.post("#sprite", "play_animation", {id = anim}) --this sends a message to the sprite (enemy) to uupdate its animation with the triggered animation
		self.anim = anim --the variable containing the current animation is now updated with the new animation
	end
end

The play_animation function has the right idea (only trigger a new animation if it’s not the same as the one currently running). But before you call that in handle_player_contact, you send a message to the sprite directly anyway, effectively overriding the play_animation function.

It also looks like you are trying to sequence the two animations by first triggering walk, then attack. That won’t work because they are both triggered simultaneously. You need to trigger the attack animation when there is contact, and trigger the walk animation when appropriate (e.g. when the attack animation finishes).

3 Likes

Do you how i can impliment this becuase i have no clue?

Just take another look at the handle_player_contact function in the tutorial you follow.

1 Like

in the toutorial he missed out a few steps that are required for it to work. As player damage is taken away whyen a specific enemy attacking frame is played. But in mine the attack animation doesn’t play until the player has left the collision space. Does anyone reccomend any other toutorials to help as i’m really stuck