Hello, I am wrathguy78 and I am making a game where you have to shoot enemies to win! I made the AI so the enemies follow the player but they are only heading right. This is the code:
function enemy.AI(dt)
for i,v in ipairs(enemy) do
if player.x + player.width / 2 < v.x / 2 then
if v.xvel > -enemy.speed then
v.xvel = v.xvel - enemy.speed * dt
end
end
if player.x + player.width / 2 > v.y / 2 then
if v.xvel < enemy.speed then
v.xvel = v.xvel + enemy.speed * dt
end
end
end
end
1) There is probably no reason to divide enemy x and y by 2 when comparing it with centre position of the player (if there is the there is the issue )
2) you compare x and y but then only update xvel of the enemy, not sure if enemy is list of enemies (suggested by the loop) or single one (suggested by the second part), I encourage you to name tables plural so it's easier to read.
1) There is probably no reason to divide enemy x and y by 2 when comparing it with centre position of the player (if there is the there is the issue )
2) you compare x and y but then only update xvel of the enemy, not sure if enemy is list of enemies (suggested by the loop) or single one (suggested by the second part), I encourage you to name tables plural so it's easier to read.
1 was the issue thanks! xD Idk why either. I changed file sprites so that could have been the problem