How to Build an Infinite Runner Engine from Scratch
Building an endless runner isn't about actually building an infinite level—it's about an illusion.
The Illusion of Infinite Movement
Instead of moving the player forward in world space, we lock the player perfectly in place along the Z-axis, and we move the world backwards. Alternatively, you can move the player and dynamically instantiate chunks of road ahead of them, destroying chunks that fall behind the camera.
Object Pooling for Obstacles
If you instantiate and destroy obstacle objects constantly during runtime, your garbage collector will eventually lock up the main thread causing a micro-stutter. In a fast-paced game, a micro-stutter often means the player dies unfairly.
Solution: Use Object Pooling. Create 100 variations of barriers, cars, and coins when the game starts, set them to SetActive(false), and pull them from your List<> when needed. When they pass the camera, deactivate them rather than calling Destroy().
Camera Follow Mechanics
A stiff camera feels amateur. To make the runner feel premium:
- Introduce subtle camera FOV shifting when the player dashes or speeds up.
- Add slight camera shake on impacts to deliver game juice.
- Keep the angle fixed with an offset strictly locked to the rigid body's smooth damp position.