Workshop › Missile Command-style Defense › Step 6 of 35
Every missile in this game does the same thing: move towards a target point at a given speed. One function does it for all of them.
```js
// Move a point `speed` pixels towards its target; true when it has arrived.
function stepTowards(m, speed) {
const dx = m.tx - m.x
const dy = m.ty - m.y
const distance = Math.hypot(dx, dy)
if (distance <= speed) {
m.x = m.tx
m.y = m.ty
return true
}
m.x += (dx / distance) * speed
m.y += (dy / distance) * speed
return false
}
```dx, dy is the arrow from the point to its target; Math.hypot is its length (Pythagoras).true.(dx, dy) / distance is a direction of length 1 (a unit vector); times speed it is exactly one step.Leave an empty line under the reset function and write stepTowards with its comment.
Skills: Velocity and gravity, Functions
← Rubble Launch an enemy missile →