Get distance along spline from world location

I’ve solved this problem perfectly, so I can get a spline line length from the world position. 100% accurate. My QQ, 78848114.

I have more precise way based on the proposed one above.
The idea is:

  1. Find 2 distances (for prev and next spline points)
  2. Get locations at this points.
  3. Calculate Alpha as (Actor-Prev).VectorLength / (Next-Prev).VectorLength
  4. Lerp the Prev distance to the Next distance using the Alpha.

In the blueprint below i also draw a debugging sphere at the location taken from the distance.
Tested that on not a very curved spline, it works there quite precise.
Screenshot:
(Can’t upload yet)

Code (it is possible to copy from there, but i will not say that the resource is secure - i just don’t know):

1 Like

I have more precise way based on the proposed one above.
The idea is:

  1. Find 2 distances (for prev and next spline points)
  2. Get locations at this points.
  3. Calculate Alpha as (Actor-Prev).VectorLength / (Next-Prev).VectorLength
  4. Lerp the Prev distance to the Next distance using the Alpha.

In the blueprint below i also draw a debugging sphere at the location taken from the distance.
Tested that on not a very curved spline, it works there quite precise.
Screenshot:

Code (it is possible to copy from there, but i will not say that the resource is secure - i just don’t know):

I can’t understand why use location, because I am a art asset maker (probably),Start learning Ue4 in 2021, poor english ,sorry 。 I use “find input key” , then get Spline point ,prev one and next one,use “get distance along spline at spline point ”to get two distance values, then let them subtract,so get two spline point length。“fraction” the input key,multiply two point ’s length and plus prev point‘s distance。 in Ue4.24
[Get Distance At Spline Closest to World Location posted by Freedomist | blueprintUE | PasteBin For Unreal Engine 4][1]

I am using linear interpolation between spline keys. We know all about location, but we know only 2 distances. So I assume that the part of the spline between 2 spline keys is quite linear.

Just draw a sphere on a calculated distance along a spline and compare results.
I did not get the point about fraction - I really have no idea how to use it and when I was trying to use the solution above - calculated distance had huge inaccuracy and was jumping time to time between some reasonable location and the previous spline key distance.

Just adding my two cents since some people seem to be misunderstanding:
when you find an Input Key and not a Spline Point it’s going to be a float, meaning it can be inbetween keys. At that point all you need is to call get distance at input key.
In my case:


auto InputKeyAppropximated = CameraSplineComponent->FindInputKeyClosestToWorldLocation(Character->GetActorLocation());
auto DistanceAlongSpline = CameraSplineComponent->GetDistanceAlongSplineAtSplineInputKey(InputKeyAppropximated);