I don’t know if I got you, but I wrote a component that could help you. This component describes a path using a array of points and has a method `GetClosestPosition`

that returns the closest point on the path, so you can define the damage by the distance between this point and player position. If you’re gonna copy and paste this component to your project, see how it’s works (there’re some Gizmos that could help you figure out how it works) add some points to the path and assign a player transform.

(This is not the best code, I’d not use `GetClosestPosition`

inside a Update method, but works)

```
class Path : MonoBehaviour {
public Vector3[] points = new Vector3[0];
public Transform player;
private void OnDrawGizmos() {
const float sphereSize = 0.2f;
if (points.Length > 0) {
Gizmos.DrawSphere(points[0], sphereSize);
for (int i = 1; i < points.Length; i++) {
Gizmos.DrawSphere(points[i - 1], sphereSize);
Gizmos.DrawSphere(points*, sphereSize);*
```

*Gizmos.DrawLine(points[i - 1], points*);**

*}*

*if (player) {*

*Gizmos.color = Color.red;*

*var cloesestPosition = GetClosestPosition(player.position);*

*Gizmos.DrawSphere(player.position, sphereSize);*

*Gizmos.DrawSphere(cloesestPosition, sphereSize);*

*Gizmos.DrawLine(cloesestPosition, player.position);*

*Gizmos.color = Color.white;*

*}*

*}*

*}*

*public Vector3 GetClosestPosition(Vector3 position) {*

*if (points.Length == 0) {*

*return position;*

*}*

*return GetClosestPosition(GetClosestPoint(position), position);*

*}*

*private Vector3 GetClosestPosition(int closestIndex, Vector3 position) {*

*Assert.IsTrue(closestIndex >= 0 && closestIndex < points.Length);*

*if (points.Length == 1) {*

*return points[0];*

*}*

*if (closestIndex == 0) {*

*return GetClosestPointOnLineSegment(points[closestIndex], points[closestIndex + 1], position);*

*}*

*else if (closestIndex == points.Length - 1) {*

*return GetClosestPointOnLineSegment(points[closestIndex], points[closestIndex - 1], position);*

*}*

*var a = GetClosestPointOnLineSegment(points[closestIndex], points[closestIndex + 1], position);*

*var b = GetClosestPointOnLineSegment(points[closestIndex], points[closestIndex - 1], position);*

*var ad = (position - a).sqrMagnitude;*

*var bd = (position - b).sqrMagnitude;*

*return ad < bd ? a : b;*

*}*

*private int GetClosestPoint(Vector3 position) {*

*int closestPointIndex = 0;*

*float closestSqrtDistance = float.MaxValue;*

*for (int i = 0; i < points.Length; i++) {*

*var sqrtDistance = (points **- position).sqrMagnitude;*

*if (sqrtDistance < closestSqrtDistance) {*

*closestSqrtDistance = sqrtDistance;*

*closestPointIndex = i;*

*}*

*}*

*return closestPointIndex;*

*}*

*private static Vector3 GetClosestPointOnLineSegment(Vector3 a, Vector3 b, Vector3 p) {*

*var ap = p - a;*

*var ab = b - a;*

*float dot = Vector3.Dot(ap, ab);*

*float distance = Vector3.Dot(ap, ab) / ab.sqrMagnitude;*

*if (distance < 0) {*

*return a;*

*}*

*return (distance > 1) ? b : a + ab * distance;*

*}*

*}*

*[120020-screenshot-1.png*|120020]