← Back to list

Unity Spline — Getting the Nearest Point With Constraints

So, I’ve started using GetNearestPoint to find the nearest point on the zip line spline to the player in order to determine where to jump…

Sean Duggan · 2026-06-18 20:06 · 0 claps · 2.3 min read
#unity #ziplines #fantacode-studios #spline
Open on Medium ↗
Wiki topics: 3D · Motion & 3D Design 🎮 · Gaming

Unity Spline — Getting the Nearest Point With Constraints

So, I’ve started using *GetNearestPoint* to find the nearest point on the zip line spline to the player in order to determine where to jump on, and how close the zip line is. A recent run through my demo level demonstrated an issue with this approach.

Yup, that’s my controller deciding that a zip line underneath them was the closest one, and attempting to jump up to it. Now, in some future iteration, it might make sense to provide a way to jump down to a zip line, but for now, I probably want to stick to jumping up to them. One problem, GetNearestPoint has no way to filter for the closest point in the spline. Probably the easiest solution for it is recognizing when the nearest point in a spline is too low, and doing some quick sampling to find the nearest “high enough” point. First step is to determine if indeed there is a “too low” point.

if (nearestPoint.y < localPos.y + _mininumZipLineHeight)
{
    // See if there's a better point that is high enough
    float bestTValue = zipline.GetNearestPointAbove(localPos, _mininumZipLineHeight);
    if (bestTValue >= 0)
    {
        float3 nearest;
        if (zipline.EvaluatePoint(bestTValue, out nearest, out _, out _))
        {
            nearestPoint = nearest;
        }
    } else
    {
        // Skip this one
        continue;
    }
}

For the GetNearestPointAbove function, we do the sampling I mentioned above.

public float GetNearestPointAbove(Vector3 playerPos, float heightBuffer = 0.5f)
{
    float bestT = -1f;
    float minDistance = float.MaxValue;

    // Divide the spline into a number of steps for sampling
    int steps = 20;
    for (int i = 0; i <= steps; i++)
    {
        float t = (float)i / steps;
        Vector3 pointOnSpline = _splineContainer.EvaluatePosition(t);

        // Constraint: Check if the point is above the player
        if (pointOnSpline.y > playerPos.y + heightBuffer)
        {
            float dist = Vector3.Distance(pointOnSpline, playerPos);
            if (dist < minDistance)
            {
                minDistance = dist;
                bestT = t;
            }
        }
    }

    return bestT; // Returns the t value of the best point found above the player
}

And, it does seem to work.

This does also remind me that I ought to rotate the player toward the target area while I’m “winding up” the jump

This does also remind me that I ought to rotate the player toward the target area while I’m “winding up” the jump

In retrospect, I probably could have cribbed from the actual Unity Spline function for getting the nearest point…

public static float GetNearestPoint<T>(T spline,
    float3 point,
    out float3 nearest,
    out float t,
    int resolution = PickResolutionDefault,
    int iterations = 2) where T : ISpline
{
    float distance = float.PositiveInfinity;
    nearest = float.PositiveInfinity;
    Segment segment = new Segment(0f, 1f);
    t = 0f;
    int res = math.min(math.max(PickResolutionMin, resolution), PickResolutionMax);

    for (int i = 0, c = math.min(10, iterations); i < c; i++)
    {
        int segments = GetSubdivisionCount(spline.GetLength() * segment.length, res);
        segment = GetNearestPoint(spline, point, segment, out distance, out nearest, out t, segments);
    }

    return distance;
}

It’s very similar to what I’m currently doing, and I feel like I’m duplicating effort here.

Postscript

This is also making me realize that I probably should scan for other situations, like whether there is a barrier between us and the nearest spline… as well as the note I made on the image above that I ought to have the player rotate toward the spline, and then jump instead of having them jump sideways.


메타데이터
post_id
2033e9e062c4
slug
unity-spline-getting-the-nearest-point-with-constraints-2033e9e062c4
url
https://medium.com/@sean.duggan/unity-spline-getting-the-nearest-point-with-constraints-2033e9e062c4
canonical_url
https://medium.com/@sean.duggan/unity-spline-getting-the-nearest-point-with-constraints-2033e9e062c4
author_url
https://medium.com/@sean.duggan
status
ok
fetched_at
2026-06-20 20:29:01