Consistent Object Spacing Despite Frame Rate and Fast Object Speeds

I’m currently developing an endless runner Unity VR game where the player stays stationary as the objects move towards them at a speed that slowly accelerates over time. I need to guarantee that the incoming objects are placed at a precise 5-meter spacing from the previously spawned obstacle, even in scenarios involving frame rate variations and variable object movement speeds. My existing code spawns an object in the next frame if the distance between the obstacle spawner and the last object is greater than or equal to 5 meters. However, I want to ensure that objects are always positioned exactly 5 meters apart, even when multiple objects need to be spawned in a single frame due to a fast speed or frame rate drops between devices.

I am still fairly new to Unity and C# so any help or guidance would be greatly appreciated!
Thank you :slight_smile:

Keep a spawn location.

Keep another point ahead of the player (say “player plus X distance”)

if the spawn location is behind the “player plus X” distance:

  • spawn a new thing

  • advance spawn location by precisely 5

Put that last if() check in a while loop if you contemplate REALLY fast player motion, otherwise if your player moves more than 5 units in a frame, he might outstrip your spawner.