In the enemy i have made i would like it to stop moving when it comes into contact with the player. Right now when it collides with the player it carries on moving after it plays the attack animation, do you guys know how i could get it to stop moving when it collides with the player and then get it to start moving when the player moves out of its collision area
You are not sharing any code so we do not know how you are moving the enemy. This makes it hard to give any advice. Are you moving it in a script in update()? Are you moving it using go.animate()?
I’m using an update function.
function update(self, dt)
if self.enemy_contact == false then
local target_speed = self.movement * max_speed --this calculates the enemies target speed based on the its current speed and the maximum
local speed_diff = target_speed - self.velocity.x --this finds the difference between the current and target speed
local acceleration = vmath.vector3(0, gravity,0) --this variable holds the current acceleration of the enemy
if speed_diff ~= 0 then --this if statement runs if the there is a difference in current and target speed
if speed_diff < 0 then --this will be true if the enemy hits the wall
acceleration.x = -move_acceleration --causes the enemy to move in the opposite direction
else
acceleration.x = move_acceleration --causes the player to move in the current direction
end
end
local dv = acceleration * dt --this calculates the velocity change in the current frame
if (math.abs(dv.x) > math.abs(speed_diff)) then --if the change in velocity is greater than the speed change the statement will run
dv.x = speed_diff --the speed difference overwrites the dv
end
local v0 = self.velocity --this assigns the players velocity to v0
self.velocity = self.velocity + dv --is creates a new velocity by adding the current velocity to the change
local dp = (v0 + self.velocity) * dt * 0.5 --this calculates the translation of the current frame on the screen
update_animation(self)
go.set_position(go.get_position() + dp) --this applies the transplation to the enemy
self.correction = vmath.vector3() --this rests the functions variables so they can be reused when called again
self.ground_contact = false
self.wall_contact = false
end
end
this is the code used to move the enemy
Ok, so you have some collision handling code in on_message()
right? So what you want to do is to set a flag when you collide with the player, something like:
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
-- check if collision was with the player (make sure its collision group is set to "player"!)
if message.other_group == hash("player") then
-- set flag
self.player_collision = true
end
end
end
Next you need to use the self.player_collision
flag in your update() logic where you move the enemy:
function update(self, dt)
-- only move player if not colliding with player
if not self.player_collision then
-- your movement code here
end
-- reset flag (it will be set again next frame if you are still colliding with the player
self.player_collision = false
end