I have a single lane road and I want cars to drive along it. I want variables such as car speed and gap lengths between cars to be changeable.

I have already implemented this simply using a Waypoint circuit along the road and spawning cars at one end of the road every few seconds, destroying them once they get to the end of the road. I am also able to change the speed of the cars.

However for choosing a gap length between cars, this implementation isn't great. I thought I could use the spawn time (e.g. 3 seconds between cars means a certain gap length) but this isn't accurate when you factor in realistic acceleration time and the speed. For instance, if the speed is 100mph and cars are spawned every 3 seconds, the first car spawned is going accelerate quickly and be a lot farther than the second car when it spawns. If the speed was 30mph with a 3 second spawn time, the cars are going to have a much smaller gap. I want to be able to predetermine what this gap is going to be regardless of the speed limit.

Any ideas how I can go about this? An idea of mine was that I could populate the road with cars, making sure that the length between the cars are the predetermined gap length and begin the simulation from there. But how do I make these cars follow the same Waypoint system? And how do I properly spawn new cars to keep a constant flow of traffic as the cars at the end of the road are destroyed?

  • 1
    \$\begingroup\$ If your goal is to portray the steady state of a stretch of road, do you need to spawn the cars at rest and have them accelerate up to speed? Or could they simply spawn already travelling at their normal speed, as though we're observing them somewhere in the middle of their journey, rather than at their start? (This kind of Last Thursdayism is pretty common in game simulations) \$\endgroup\$ – DMGregory Apr 3 '18 at 3:05
  • \$\begingroup\$ Spawning cars already travelling at the desired speed is definitely an avenue I can take. I'm using the CarAIControl and CarController standard asset scripts at the moment, I assume it's possible to set the speed without having to accelerate to it? I'll have to look into this. \$\endgroup\$ – rafvasq Apr 3 '18 at 3:56
  • \$\begingroup\$ @DMGregory Another thing is that I don't know if it's possible to have cars follow the waypoint system if they spawn in the middle of it. I've played with this and a car spawned in the middle of the road always turns around and tries to drive to the beginning of the road (where the waypoint system begins) \$\endgroup\$ – rafvasq Apr 3 '18 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.