# Find spline key location along specific axis

**URL:** <https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160>\
**Category:** World Creation\
**Tags:** distance, unreal-engine, Spline, CPP, question\
**Created:** [April 28, 2017, 12:50pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160 "2017-04-28T12:50:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_0757868c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0757868c](https://forums.unrealengine.com/u/anonymous_user_0757868c)\
**Post date:** [April 28, 2017, 12:50pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/1 "2017-04-28T12:50:53Z")

</div>

I’m trying to do something similar to FindLocationClosestToWorldLocation, but instead of finding the closest point in 3D space, I want to find the closest point along the Z axis. Is there some way to accomplish this or would I have to extend USplineComponent somehow?

Speaking of which, can you extend UObjects like USplineComponent without building from source?

---

<div class="post-metadata">

**Author:** ![Benergy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Benergy](https://forums.unrealengine.com/u/Benergy)\
**Post date:** [April 28, 2017, 12:58pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/2 "2017-04-28T12:58:43Z")

</div>

Do you want to find the point on the z axis that is

1. closest to a specific point on the spline **or**
2. closest to _all_ points on the spline

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0757868c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0757868c](https://forums.unrealengine.com/u/anonymous_user_0757868c)\
**Post date:** [April 28, 2017, 2:17pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/3 "2017-04-28T14:17:40Z")

</div>

No its more like this in the picture:

 ![135942-spline.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/c/a/aca4eceff36ceb78a1ee9e959f30ce1ac3eb3e8d.png)

The black point is my location in the world. The red line would be what FindLocationClosestToWorldLocation would do, but I want the blue line, the closest location on the z axis (which may or may not exist of course depending on the spline).

Edit: Just another rudimentary example to show what I mean in 3D:

 ![135945-spline2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/d/5/3d57e6a978cfc2ef885c8197301b5f0bc6d4852d.png)

So the red line might be the closest point on the spline to my location, but the blue line is the closest point along an axis, in this case I made it the y axis. Or really I should say, I’m taking the YZ plane cutout from my location, and finding the closest point in the spline that intersects with the plane.

---

<div class="post-metadata">

**Author:** ![42EspoiR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@42EspoiR](https://forums.unrealengine.com/u/42EspoiR)\
**Post date:** [April 28, 2017, 3:53pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/4 "2017-04-28T15:53:53Z")

</div>

I don’t see better way than iterate on all points of your spline…  
Then you use the function [GetPointDistancetoLine](https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Vector/GetPointDistancetoLine/index.html) and choose the best result.  
With:

1. Point: the point on your spline
2. Line: Your axis

---

<div class="post-metadata">

**Author:** ![Benergy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Benergy](https://forums.unrealengine.com/u/Benergy)\
**Post date:** [April 28, 2017, 4:08pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/5 "2017-04-28T16:08:23Z")

</div>

I looked into the source code but unfortunately the algorithm used to find the closest point on a spline to a location is beyond my mathematical skills. But I saw that they used an “inaccurate” algorithm, i.e. they try to get closer to the nearest point by iterating. So I would do the same as suggested by 42EspoiR, iterating over a certain amount of points with a predefined delta distance between them, but use FVector::PointPlaneDist instead.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0757868c](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0757868c](https://forums.unrealengine.com/u/anonymous_user_0757868c)\
**Post date:** [April 28, 2017, 5:52pm UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/6 "2017-04-28T17:52:43Z")

</div>

Yea I just looked at that as well. I think I can just do something similar to what they did within my own code, since I can just use GetSplinePointsPosition to get the FInterpCurveVector and then go from there and use my own version of InaccurateFindNearest for planes.

---

<div class="post-metadata">

**Author:** ![TheWolf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thewolf/32/1036665_2.png) [@TheWolf](https://forums.unrealengine.com/u/TheWolf)\
**Post date:** [January 30, 2024, 4:12am UTC](https://forums.unrealengine.com/t/find-spline-key-location-along-specific-axis/392160/7 "2024-01-30T04:12:40Z")

</div>

Thanks! 2024 and this solution is still rocking. Wondering if it is still the best / only one out there?
