In my game I have a number of enemies who follow a critter. What I would like is for the enemies to auto rotate so that they are always facing the critter.
I work out the angle towards the critter in order for the enemies to move in a direct line by using the math.atan2 function and this works perfectly. I then use the same angle to work out how much to rotate the enemy by subtracting the old angle from the new one and using that as the new angle delta.
However, the enemies will not rotate at all. I can’t see any reason why this would be so.
Here’s my code for updating the enemies…
[lua]function updateEnemy()
for f=1, #enemies do
angle= math.atan2(critter.y - enemies[f].y, critter.x - enemies[f].x) – work out angle between enemy and critter
enemies[f].x = enemies[f].x + (math.cos(angle) * enemies[f].speed) – update x pos in relation to angle
enemies[f].y = enemies[f].y + (math.sin(angle) * enemies[f].speed) – update y pos in relation to angle
if (enemies[f].spec == 1) or (enemies[f].spec == 2) then – Species 1 & 2 rotate constantly
enemies[f]:rotate(5)
end
if (enemies[f].spec == 3) or (enemies[f].spec == 4) then – Species 3 & 4 rotate to face the critter
angledelta = angle - enemies[f].oldangle – How many degrees to rotate the enemy
enemies[f]:rotate(angledelta) – Rotate enemy
enemies[f].oldangle=angle – Store the angle for next use
end
end
end [/lua]
Any help at all would be very very appreciated.
Cheers [import]uid: 7841 topic_id: 14439 reply_id: 314439[/import]
