Hi everyone this is my first time posting here because I’m pretty new to Defold and Lua. I’m trying to write a script for the enemy in my game (2D Top down rouge like). I’m trying to get the enemy to chase the player with the following code:
function update(self, dt)
--for some reason pos DOES update every single frame but playerPos does NOT update every frame, only the first
--if this gets fixed then the enemy moving towards player will work
local playerPos = go.get_position("/player")
local pos = go.get_position()
print(playerPos)
--move up
if pos.y<playerPos.y then
self.vel.y = 40
end
--move down
if pos.y > playerPos.y then
self.vel.y = -40
end
--move left
if pos.x > playerPos.x then
self.vel.x = -40
sprite.set_hflip("#sprite", true)
end
--move right
if pos.x < playerPos.x then
self.vel.x = 40
sprite.set_hflip("#sprite", false)
end
pos = pos + self.vel * dt
go.set_position(pos)
self.vel.x = 0
self.vel.y = 0
self.correction = vmath.vector3()
end
When I print(pos) the enemy’s position is printed to console every frame. When I print(playerPos) it only prints the player’s initial position and does not update every frame. The enemy moves towards that initial position and then stops there. If anybody has any suggestions it would be greatly appreciated!