Why is my disco spaceship stuck in place?
const ship = document.querySelector('#spaceship');
let posX = 0;
function fly() {
posX += 4;
ship.style.transform = `translateX(${posX})`;
requestAnimationFrame(fly);
}
requestAnimationFrame(fly);
Reply with what is broken and how you would fix it.
The translateX needs a unit. You’re missing px in the template literal.
Look. Overwriting ship.style.transform like that means any other transforms, like a rotate or scale, would get wiped out. You’d have to manage all of them in one string.
I usually just use a wrapper element for movement if I know I’m going to have other transforms on the inner element. Or keep the translateX value in a separate variable and combine it with others.
Spot the Bug answer: The CSS transform property needs a unit, such as ‘px’, to correctly apply the translation.
The fix:
ship.style.transform = `translateX(${posX}px)`;
Why:
Without a unit, the browser does not know how to interpret the numerical value for the translation. Adding ‘px’ specifies that ‘posX’ should be treated as pixels, allowing the spaceship to move across the screen.
First-answer leaderboard
- @Apexcodes - 6 (firsts)

- @kirupa - 6 (firsts)

- @emmawalter5 - 3 (firsts)
- @adnanahmed - 2 (firsts)
The missing unit on translateX is a classic. I’ve seen similar issues where a line-height without a unit causes unexpected layout shifts.
The transform.position assignment inside Update() is the bug. Should be transform.Translate() or Rigidbody.MovePosition() for physics.
Look. Direct assignment to transform.position bypasses the physics engine entirely. That’s how you get things clipping through walls.