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

Make a car follow a racetrack loop (multiple times), at constant speed.

Discussion in 'General Discussion' started by Dynaflow1949, Aug 2, 2021.

  1. Dynaflow1949

    Dynaflow1949

    Joined:
    Jun 11, 2021
    Posts:
    3
    Hello,
    I am new to Unity and don't know much about C#.

    My project:
    Create a racetrack loop, which the car enters and then follows multiple times. Player sits in the car with VR glasses on

    What I tried:
    Animate the car along the Racetrack ( difficult to get same speed along the whole route)
    I imported this Splines Tutorial File but it didn't work and I think there should be a easier way.


    How do I get the car to enter the racetrack and then follow it at constant speed?
    Thank y'all!
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,553
    If you want the car to DRIVE by itself, make a basic car AI that drives it to a waypoint. Then make a list of waypoints.

    If you want an animation that you can control, then animate car movement in an editor and import it into unity. And yes, splines could work too.
     
  3. Dynaflow1949

    Dynaflow1949

    Joined:
    Jun 11, 2021
    Posts:
    3
    Thank you for the fast response!

    The car doesnt have to drive by itself. just going around and around would be enough. However, Do you maybe have Video or Tutorial on how this AI could look like and work?

    What do you mean by editor, like in blender?
    I would prefer to do it in Unity itself, to have more flexibility.
    Thanks again
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,553
    The absolute dumbest AI that could drive to waypoint would be something like this:

    Given a waypoint, for every frame:
    1. calculate vector to waypoint.
    toWayPoint = waypointTransform.position - pointBetweenFrontWheels.transform.position
    .
    2. Calculate steering angle you'd need to set front wheels to using Mathf.Atan2(y, x). For "x" argument use dot product between car's forward vector and vector to waypoint, for "y" argument use dot prouct between car's LEFT vector and vector to waypoint. That assumes that wheels turn counterclockwise. Limit result by something like +- 45..60 degrees.
    3. Start driving forward.

    4. When close to current waypoint, set the current waypoint to the next one.

    And that's it.

    This sort of "AI" is extremely dumb, can't reverse, will start driving in circles if it overshoots the waypoint, but if you carefully position the points by hand, it will follow the route.

    Yes, just animate car movement on track and import.

    In this case you'd need to implement a spline (either Bezier or catmull-rom) and spline following algorithm.
    It is a less than a day of work for an experienced programmer, and there's likely an asset for that on asset store. Somewhere.
    https://en.wikipedia.org/wiki/Centripetal_Catmull–Rom_spline
     
  5. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    https://assetstore.unity.com/packages/tools/animation/simple-waypoint-system-2506 has components that would get you up and running in 5 minutes max. Literally just press P key to make nodes at your mouse position to create a spline on your track, drag your choice of mover script to your car, supply the path, and it'll move and rotate along it with a variety of options (loop, ping pong, once, etc), if the car isn't already on the path it can navigate to it automatically.

    Can also set events that fire when they reach a node, like maybe after the first lap you want them to switch to a different path, super trivial.
     
  6. Dynaflow1949

    Dynaflow1949

    Joined:
    Jun 11, 2021
    Posts:
    3
    Hello Guys,
    Thanks again for your support, I was able to create the car movement I wanted. However, in the Next step I would like to have more control over the car's speed. it should accelerate on the straight parts of the tracks and slow down in the corners.
    How would I go about doing that? would I do that with a mover script, as Imbarns suggested?
     
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,553
    If the car is riding the spline through control points by directly using "t" parameter (t == 0 means start of the segment, and t == 1 means end of the segment), then to make it go faster simply place control points further apart.

    I the car is using physical speed in some way, then look ahead of the car and check when the road starts to turn and how much it turns. Slow down based on turn sharpness.
     
  8. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    If you're really new and learning you could throw a trigger before and after turns. Like a sphere collider set as trigger. When the racer enters it you get OnTriggerEntered called, that could start a co-routine that reduces speed 3x a second over 1-2 seconds so its not all at once. And vice versa when you hit the speed up trigger it increases speed over time.


    Otherwise if you have an array of waypoints you're sequentially going through, or a spline, you can calculate the direction/angle between the current target waypoint/point and 1-3 ahead of it.

    Sometimes useful is also checking if the player is facing the direction the race should be going using dot product between their forward direction and the target waypoint. If a player turns around and starts going the wrong way, the dot product will be negative instead of positive, if they're facing 180 degrees completely backwards it will be -1. If they need to turn 90 degrees the dot product will be 0, so you can check the dot product of the next waypoint to ensure they're facing the right direction, then the dot product of 2 points ahead and the further from positive 1 towards 0 the greater the turn coming up. Recap: a dot product of 1 is directly ahead, dot product of 0 is perpendicular to the direction we're looking, and at -1 we're facing the exact opposite direction we should be facing.