My enemy character keeps walking into the same wall over and over again, i have attached the code that is supposed to fix this but I cant figure out the problem
I don’t see any code?
code.txt (4.3 KB)
local move_acceleration=8000
local max_speed=4000
local gravity=-1000
local ground_contact=false
local wall_contact=false
-- pre-has for system parameters to speed up process
local msg_contact_point_response=hash("contact_point_response")
local msg_animation_done=hash("animation_done")
local group_obstacle = hash("ground")
local group_wall = hash("wall")
-- animations
local anime_walk=hash("Walk")
local anim_idle=hash("Idle")
local anime_hit = hash("Hit")
local anime_attack=hash("Attack")
function init(self)
-- Add initialization code here
-- Learn more: https://defold.com/manuals/script/
-- Remove this function if not needed
-- init enemy velocity
self.velocity=vmath.vector3(0,0,0)
self.movement=1
self.anim=nil
end
function final(self)
-- Add finalization code here
-- Learn more: https://defold.com/manuals/script/
-- Remove this function if not needed
end
local function play_animation(self, anim)
if self.anim ~= anim then
msg.post("#sprite","play_animation",{id=anim})
self.anim=anim
end
end
local function update_animations(self)
sprite.set_hflip("#sprite", self.movement<0)
--go.set_rotation(vmath.quat_rotation_y(math.rad(180)))
if (self.movement<0) then
physics.set_hflip("#CollisionSkeleton", true);
physics.set_hflip("#Hitbox", true);
else
physics.set_hflip("#CollisionSkeleton", false);
physics.set_hflip("#Hitbox",false);
end
if (self.ground_contact) then
if (self.velocity.x == 0) then
play_animation(self,anim_idle)
else
play_animation(self,anim_walk)
end
end
end
function update(self, dt)
-- Add update code here
-- Learn more: https://defold.com/manuals/script/
-- Remove this function if not needed
local target_speed=self.movement*max_speed
local speed_diff = target_speed-self.velocity.x
local acceleration = vmath.vector3(0,gravity,0)
if(speed_diff~=0) then
if(speed_diff<0) then
acceleration.x= - move_acceleration
else
acceleration.x = move_acceleration
end
end
local dv = acceleration * dt
if (math.abs(dv.x)>math.abs(speed_diff)) then
dv.x = speed_diff
end
local v0 = self.velocity
local v0 = self.velocity + dv
local dp= (v0 +self.velocity) * dt * 0.5
update_animations(self)
go.set_position(go.get_position() + dp)
self.correction=vmath.vector3()
self.ground_contact=false
self.wall_contact=false
end
local function handle_obstacle_contact(self,normal,distance)
local proj=vmath.dot(self.correction, normal)
local comp=(distance-proj)*normal
self.correction=self.correction+comp
go.set_position(go.get_position()+comp)
if normal.y > 0.7 then
self.ground_contact=true
end
proj=vmath.dot(self.velocity,normal)
if proj<0 then
self.velocity = self.velocity - proj*normal
end
end
local function handle_wall_contact(self,normal,distance)
local proj=vmath.dot(self.correction, normal)
local comp=(distance-proj)*normal
self.correction=self.correction+comp
go.set_position(go.get_position()+comp)
self.wall_contact=true
self.movement=normal.x
proj=vmath.dot(self.velocity,normal)
if proj<0 then
self.velocity = self.velocity - proj*normal
end
end
function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Learn more: https://defold.com/manuals/message-passing/
-- Remove this function if not needed
if message_id == msg_contact_point_response then
if message.other_group == group_obstacle then
handle_obstacle_contact(self, message.normal, message.distance)
elseif message.group == group_wall and not self.wall_contact then
handle_wall_contact(self, message.normal, message.distance)
end
end
end
function on_input(self, action_id, action)
-- Add input-handling code here. The game object this script is attached to
-- must have acquired input focus:
--
-- msg.post(".", "acquire_input_focus")
--
-- All mapped input bindings will be received. Mouse and touch input will
-- be received regardless of where on the screen it happened.
-- Learn more: https://defold.com/manuals/input/
-- Remove this function if not needed
end
function on_reload(self)
-- Add reload-handling code here
-- Learn more: https://defold.com/manuals/hot-reload/
-- Remove this function if not needed
end
I’d check the sign of the normal.x value to determine if the wall was to the left or right of the enemy and use this to flip direction of movement. Something like this in your handle_wall_contact() function:
if message.normal.x > 0 then
self.movement.x = -math.abs(self.movement.x)
elseif message.normal < 0 then
self.movement.x = math.abs(self.movement.x)
end
It may need to be the other way around on the sign before math.abs() but you’ll figure it out if you test.
1 Like