# Figure out whether a point is inside a Unity spline (new spline system)

Hi all,

I tackled this problem recently and since I couldn’t find the solution on search engines, I figured I’d share my solution here where it’s visible for all:

``````/// <summary>
/// Whether a point is inside a closed spline.
/// </summary>
/// <param name="point">A point, local to the spline's space</param>
/// <param name="spline">Unity spline</param>
/// <returns>Whether the point is inside the spline</returns>
public static bool IsInsideSpline(float3 point, Spline spline)
{
SplineUtility.GetNearestPoint(spline, point, out var splinePoint, out var t);
spline.Evaluate(t, out _, out var tangent, out _);

var cross = math.cross(math.up(), math.normalize(tangent));
return math.dot(splinePoint - point, cross) < 0;
}
``````

Note: this only works if a spline is drawn clockwise.

1 Like
``````    public static bool IsInsideSpline(float3 point, SplineContainer splineContainer, Bounds splineBounds, out Vector3 nearestPointInSpline)
{
Vector3 pointPositionLocalToSpline = splineContainer.transform.InverseTransformPoint(point);

SplineUtility.GetNearestPoint(splineContainer.Spline, pointPositionLocalToSpline, out var splinePoint, out var t);
splinePoint.y = pointPositionLocalToSpline.y;

if(Vector3.Distance(point, splineContainer.transform.TransformPoint(splineBounds.center)) < Vector3.Distance(splinePoint, splineBounds.center))
{
// If point is inside of the spline...
nearestPointInSpline = point;
return true;
}
else
{
nearestPointInSpline = splineContainer.transform.TransformPoint(splinePoint);
return false;
}
}
``````

I needed to move an Audio Emitter unto the listener inside of the spline area and this code seemed to have worked. Don’t know how efficient it is, but since this is a related problem thought I would leave it here!

This goes wrong when the spline’s shape is concave. The dot product in my implementation is what resolves this but that means it relies on the flow direction of the spline.