Problem:
It overshoots, whenever a faster enemy is "moving", it overshoots.
How do i clamp the velocity, so it doesn't overshoots anymore. IM not good with math stuff.
EnemySpeed when testing: 0.1
private moveAlongPath(entity: Entity): void {
const move = () => {
if (Game.gameStatus === GameStatus.LOST || !this.enemyMap.has(entity)) return;
const enemy = this.enemyMap.get(entity)!;
const { pathGroupIndex, waypointIndex } = enemy;
const enemyPath = this.enemyPathGroups[pathGroupIndex];
if (waypointIndex >= enemyPath.length) {
enemy.reachedExit();
this.removeEnemy(enemy);
return;
}
const target = enemyPath[waypointIndex];
const position = entity.location;
// get relative distance
const direction = {
x: target.x - position.x,
y: target.y - position.y,
z: target.z - position.z
};
// get distance
const distance = Math.sqrt(
direction.x ** 2 +
direction.y ** 2 +
direction.z ** 2
);
// if we're close enough, move to the next waypoint
if (distance < 0.2 /* epsilon */) {
enemy.waypointIndex += 1;
this.enemyMap.set(entity, enemy);
system.run(move);
} else {
const velocity = {
x: direction.x / distance * enemy.speed * EnemyManager.tickDelay,
y: direction.y / distance * enemy.speed * EnemyManager.tickDelay,
z: direction.z / distance * enemy.speed * EnemyManager.tickDelay
};
entity.applyImpulse(velocity);
entity.lookAt(target);
system.runTimeout(move, EnemyManager.tickDelay);
}
};
move();
}