跟随玩家时敌人的速度

时间:2015-11-01 22:23:16

标签: java angle motion

所以我之前有这段代码,显然非常熟悉。它目前将我添加到arraylist的所有僵尸移动到我的玩家,但如果僵尸是玩家的正面(向右),那么僵尸的移动速度比在左边移动得快。有任何想法如何修复?

for (Zombie zombie : zombies) {
    zombie.distX = Game.player.x - zombie.x;
    zombie.distY = Game.player.y - zombie.y;

    zombie.angle = Math.atan2(zombie.distY, zombie.distX);

    zombie.x += Math.cos(zombie.angle) * zombie.speed;
    zombie.y += Math.sin(zombie.angle) * zombie.speed;
}
编辑:我做了这些更改,使其正常化,现在它可以正常工作!谢谢!

zombie.angle = Math.sqrt(Math.pow(zombie.distX, 2) + Math.pow(zombie.distY, 2));
zombie.distX /= zombie.angle;
zombie.distY /= zombie.angle;

zombie.x += zombie.distX * 2;
zombie.y += zombie.distY * 2;

0 个答案:

没有答案