Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Object following a spline at distance

Discussion in 'Scripting' started by EarthHobbit, Aug 9, 2023.

  1. EarthHobbit

    EarthHobbit

    Joined:
    May 10, 2016
    Posts:
    41
    I would like to have different GameObjects following a spline, but at a given distance. Like on the drawing below where my spline is in red, and I want the boxed GameObjects to follow the same path at the same distance they had initially from the spline. The left boxes should reach the end before the right ones because path is shorter there.
    upload_2023-8-9_8-44-38.png
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This can be tricky. What you will need to do is some kind of a solver to ensure that the offset paths are truly traversed at a higher rate than the core path.

    By solver I mean you would move ahead the correct amount on the spline, see how much the "beside" object moved, then adjust the spline motion up a bit, down a bit, hone in on it with a binary search basically.

    Keep in mind splines can be arbitrarily tangled, so technically the naive "follow beside" position could even go backwards for portions of the inside of a very tight turn.
     
  4. EarthHobbit

    EarthHobbit

    Joined:
    May 10, 2016
    Posts:
    41
    Thanks. I started investigating this option. Where I am struggling, is that I parse each knot on the spline, and now need to evaluate the tangent of this knot. However the spline.Evaluate function (which gives this tangent) requires as first argument a number between 0.0 and 1.0; How can I find this number from the knot ? ie if I take first knot, number is 0.0, last knot is 1.0, but intermediate knots ? I assume it's not 0.25, 0.5 and 0.75, because knots maybe be close to each other.
    Thanks !
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    See my comment: you need a solver to find this.
     
  6. EarthHobbit

    EarthHobbit

    Joined:
    May 10, 2016
    Posts:
    41
    Thanks. Solver concept for spline is unknown to me (only known for Physics). Any guidance for me ?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Generally a solver is used to evaluate a non-linear function by subdivided guessing.

    Google for stuff like "bezier curve by distance" for what I mean. You can usually just take someone else's and hack it, then test it to make sure it works.