using System.Collections;

using System.Collections.Generic;

using UnityEngine;

```
const int maxIterations = 100;
const float minAcceptableDst = 0.01f;
public void Solve(Vector3[] points, Vector3 target)
{
Vector3 origin = points[0];
float[] segmentLenghts = new float[points.Lenght - 1];
for (int i = 0; i < points.Lenght - 1; i++)
{
segmentLenghts _= (points[i + 1] - points*).magnitude;*_
```

*}*

*for (int iteration = 0; iteration < maxIterations; iteration ++)*

*{*

*bool startingFromTarget = iteration % 2 == 0;*

*System.Array.Reverse(points);*

*System.Array.Reverse(segmentLenghts);*

*points[0] = (startingFromTarget) ? target : origin;*

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

*{*

*Vector3 dir = (points - points[i - 1]).normalized;*

*points**= points[i-1] + dir * segmentLenghts[i-1];*

*}**float dstToTarget = (points[points.Length - 1] - target).magnitude;*

*if (!startingFromTarget && dstToTarget <= minAcceptbleDst)*

*{*

*return;*

*}*

*}*

*}*