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