Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best way to tackle an endless road?

Discussion in 'General Discussion' started by derkoi, Sep 10, 2012.

  1. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    I'm thinking of making a driving game with an endless road, perhaps just straight.

    I'm thinking of having a few spawn points above the camera off screen that randomly spawns roadside objects like trees and a collider that is off screen behind the camera that once an object passes through the collider it despawns. I think that method should work.

    The problem I'm facing is how to spawn the road segments so they all line up? I was thinking of spawning them at a spawn point and adjust the position of the spawn point until the segments line up.. :confused:

    Any suggestions?
     
  2. blurnie

    blurnie

    Joined:
    Sep 10, 2012
    Posts:
    2
    Personally i would just make the road a circle, this way you only have to worry about the random spawn points around it, or you could just make a long straight road and make your car go back to the start after a while,

    hope this helps you ;)
     
  3. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    Interesting, but if the road was a circle, wouldn't they need to drive on a corner all the time and it would look like a corner too?

    Do back to the start isn't a bad idea, but I'm not sure how I'd disguise the jump?
     
  4. blurnie

    blurnie

    Joined:
    Sep 10, 2012
    Posts:
    2
    Last edited: Sep 10, 2012
  5. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    Oh yeah, I understand, so i'd need to rotate the road as I accelerated the car.
     
  6. lpye

    lpye

    Joined:
    Aug 2, 2012
    Posts:
    30
    Something I'm going to pursue for a project of mine but which I have yet to test the feasibility of is creating a constant downward local force to lock your character down onto a surface that isn't flat, then turn off gravity.
     
  7. Swiftpaw

    Swiftpaw

    Joined:
    Aug 14, 2012
    Posts:
    24
    If you're wanting just a simple road you can do it the other way around, keep the car at the level origin and move the road instead. Then it's easy to spawn things like trees etc. offscreen too, and just go from there. You can get fancy with corners and things with a little bit of work, just got to offset the spawn positions by the previous road type so they all sync up.

    There's other ways to tackle it too if you wanted to move the car so that you get more realistic physics based movement. You can just spawn segments as you will, and have a script running in the background that 'jumps' the whole level backwards once the car gets too far off center (to prevent floating point errors). It all really depends on how you want to approach it, good luck!
     
  8. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    Thanks, yeah I really want to move the car as I'm using UnityCar 2 for the physics. :)

    Not sure I fully understand what you mean by jumping the whole level backwards once the car gets too far off center?
     
  9. Swiftpaw

    Swiftpaw

    Joined:
    Aug 14, 2012
    Posts:
    24
    Basically you could spawn a road endlessly and move along it, but eventually you'll run into problems with floating point accuracy, what i mean is you take every object in the scene, and move it all at once backwards so that it's at the origin again, relative to where it should be of course. I thought I remembered seeing a script for it on the wiki, and sure enough, there is:

    http://wiki.unity3d.com/index.php/Floating_Origin

    That page should explain it better than i am!
     
  10. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    Thanks Swiftpaw, I'll check that out. :)
     
  11. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    I'm having success with this set up:

    Gameobject holder with Camera and track spawn point inside.

    Holder moves with car forwards only so the camera remains over the road, the spawn point is positioned off screen in front moves with car forwards.

    Created a prefab that has a length of track and a trigger towards the end of it.

    When car goes through trigger, a track prefab is spawned at the spawn point just off screen in front. This repeats each time a trigger is triggered.

    Works great, things I want to do are:

    Pool the prefabs for more efficiency.
    Create a trigger behind the car that destroys track that has been passed.
    Create similar spawners for trees etc cars.
     
  12. Swiftpaw

    Swiftpaw

    Joined:
    Aug 14, 2012
    Posts:
    24
    Nice to see you've got something that works for you.

    Pooling the prefabs is a good idea, i've set up object pools for my project and they work great.

    The trigger is probably the best way to go about removing track pieces. I was going to suggest you could check their distance from the player and return them to the pool that way, probably not as efficient as triggers though once I thought about it heh.

    Good luck!
     
  13. wana7262

    wana7262

    Joined:
    Jul 2, 2012
    Posts:
    103
    how to do u spawn cars (Traffic) on road?