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

Racing game follow path/driveline [HOW TO]

Discussion in 'General Discussion' started by malyna, May 27, 2015.

  1. malyna

    malyna

    Joined:
    Jul 9, 2010
    Posts:
    105
    Hello,

    I'm thinking about adding a follow path to my game so player know where to drive :).
    Something like in here (1m 20s):

    or here:


    There is green line which is fading really nicely and disappear behind the car.
    Do You know how to achieve this effect? Is it some kind of custom shader or maybe a lot of little waypoints?
    I don't even know how You can call this type of follow path/drive line, to look for solution in google.

    Any help would be most welcome!
    Thanks
     
  2. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    I think the correct term would be "racing line".
    Normally you would describe a racing line using several way points and either interpolate between those (eg. Bézier curve) or use even more way points with linear interpolation.
    The main use case is as driving line for AI cars but you could also use it to visualize the racing line and to determine the current race position of each car.
    For the rendering you probably would use some kind of trail renderer (e.g. similar to the skid marks in Unity's car tutorial).
     
  3. malyna

    malyna

    Joined:
    Jul 9, 2010
    Posts:
    105
    Hi,

    Thanks BIG BUG,
    I think I know how to build the line based on spline, bezier curve etc.

    What I need to know is the driver progress through the race so i can nicely fade racing line behind the car (and reveal in front).
    Is it some kind of interpolation ? finding closest point on the racing line?
    how to check if player is driving along the line and not cheating by taking shortcuts? Should i use some control checkpoints?
    I am looking for some basic idea how to approach this problem, without spaming thousands of checkpoints :).
    Thank for your help!
     
  4. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Check this:




     
    Last edited: May 28, 2015
    steego and NomadKing like this.
  5. malyna

    malyna

    Joined:
    Jul 9, 2010
    Posts:
    105
    Hey Marceta,
    Thanks for videos, i think thay can help me to understand some path ideas,
    But AI is not my purpose for the racing line,
    I just want to visualize it like in reckless racing 3 :)
     
  6. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    457
    Ok, I see.
    Basically you need the information where a car would be on the racing line, even if it is not.
    What I did for a game a few years ago:
    -each car stores the "current waypoint" - the waypoint where a car is headed next
    -each waypoint has a (circular) trigger, which - if the car enters - sets the car's current waypoint to the following waypoint
    -to handle cases where a car misses the trigger (e.g. by driving off-track) the car's waypoint will also increase if it is closer to the next waypoint than to the current one
    -in order to decide where a car is between waypoints the distances to the next / last waypoint are compared: "distLast / (distNext + distLast)" which gives you a factor between 1 (= next waypoint reached) and 0 (= still at last waypoint)

    It was a simple solution which worked well enough for my AI cars and the calculation of the current race position.
    Even if this particular solution is probably not accurate enough in your case it may give you an idea how to approach this thing.
     
  7. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    I'd just get the closest waypoint (to player's car) and draw line from few waypoints behind closest waypoint then few in front. So basically line would be like this (a - invisible waypoints, I - visible waypoints, X - closest waypoint to player's car)

    -a-a-a-a-a-a-a-a-a-a-I-I-I-X-I-I-I-a-a-a-a-a-a-a-a-a-a-a-

    Only line going through all visible waypoints would be drawn.
    -a-a
     
  8. malyna

    malyna

    Joined:
    Jul 9, 2010
    Posts:
    105
    Big Thanks guys!
    The only thing left is to make it look really smooth when fading the racing line, I have some ideas for that,
    If I would need some more help i'll be back in here :)
     
  9. darkhog

    darkhog

    Joined:
    Dec 4, 2012
    Posts:
    2,218
    For fading I'd suggest some simple shader that would fade it to alpha based on a gradient.