Workshop › Tower Defense › Step 22 of 42
Bullets home in: every frame they move 0.3 tiles straight towards where the target is now. When the target is closer than one step, it is hit and the bullet is done.
```js
for (const b of bullets) {
const p = pointAt(b.target.d)
const dx = p.x - b.x
const dy = p.y - b.y
const distance = Math.hypot(dx, dy)
if (distance < 0.3) {
hit(b)
b.done = true
} else {
b.x += (dx / distance) * 0.3
b.y += (dy / distance) * 0.3
}
}
bullets = bullets.filter((b) => !b.done)
```dx, dy is the arrow from the bullet to the target; distance its length.dx / distance, dy / distance is a direction of length 1; times 0.3 it is one step.done and filtered out after the loop.At the end of update, after an empty line, write the bullets loop and the filter.
Skills: Velocity and gravity, Collision detection